<?php
/**
* @package Pages-and-Items (com_pi_pages_and_items)
* @version 1.5.7
* @copyright Copyright (C) 2006-2010 Carsten Engel. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
* @author http://www.pages-and-items.com
*/

//no direct access
if(!defined('_VALID_MOS') && !defined('_JEXEC')){
	die('Restricted access');
}

//HTML
class class_fieldtype_html extends class_pi{	

	function display_config_form($plugin, $type_id, $name, $field_params, $field_id){
		//description	
		$html = $this->display_field_description($field_params);
		//validation
		$html .= $this->display_field_validation($field_params);
		//validation_mesage
		$html .= $this->display_field_validation_message($field_params);		
		//default value
		$field_name = _pi_lang_default_value;		
		$field_content = '<textarea cols="100" rows="25"  name="default_value" >'.$field_params['default_value'].'</textarea>';	
		$html .= $this->display_field($field_name, $field_content);			
		return $html;
	}
	
	function params_save($params_string){		
		if(defined('_JEXEC')){
			//joomla 1.5
			//$default_value = JRequest::getVar('default_value','','post','string', JREQUEST_ALLOWHTML);	
			$default_value = JRequest::getVar('default_value','','post','string', JREQUEST_ALLOWRAW);						
		}else{
			//joomla 1.0.x					
			//$default_value = mosGetParam( $_POST, 'default_value','', _MOS_ALLOWHTML);
			$default_value = mosGetParam( $_POST, 'default_value','', _MOS_ALLOWRAW);
			$default_value = stripslashes($default_value);
		}			
		$default_value = str_replace('<br>','<br />', $default_value);
		$default_value = 'default_value-=-'.$default_value.'[;-)# ]';
		$default_temp = 'default_value-=-'.$this->get_field_param($params_string, 'default_value').'[;-)# ]';
		if(strpos($params_string, 'default_value-=-')){
			$params_string = str_replace($default_temp, $default_value, $params_string);			
		}else{
			$params_string .= $default_value;			
		}		
		return $params_string;
	}	
	
	function display_item_edit($field, $field_params, $field_values, $field_value, $new_field, $field_id){	
		//if new field, set defaults
		if($new_field){			
			$field_value = $field_params['default_value'];			
		}
		
		$html = '<div class="field_type_html fieldtype">';
		$html .= '<div class="pi_form_wrapper">';
		$html .= '<div class="pi_width20">';
		$html .= $field->name.':';		
		if($this->check_if_field_param_is_present($field_params, 'validation')){
			if($field_params['validation']){
				$html .= '<img src="'.$this->live_site.'/administrator/components/com_pi_pages_and_items/images/star.gif" alt="required field" />';
			}
		}
		$html .= '</div>';
		$html .= '<div class="pi_width70">';
		if($field_params['description']){
			$html .= '<div>'.$field_params['description'].'</div>';
		}
		if(strpos($field_value, '</textarea>')){
			$field_value = str_replace('</textarea>', '&lt;&#47;textarea&gt;', $field_value);
		}
		$html .= '<textarea class="textarea" cols="60" rows="10" name="'.$field_id.'" id="'.$field_id.'" >'.$field_value.'</textarea>';
		$html .= '</div>';
		$html .= '</div>';
		$html .= '</div>';		
		
		return $html;
	}
	
	function render_field_output($field, $intro_or_full, $readmore_type=0, $editor_id=0){		
		return addslashes($field->value);	
		//return addslashes($field->value).'soep'.$this->template_property($field,'size');	
	}
	
	function field_save($field, $insert_or_update){	
		
		$value_name = 'field_values_'.$field->id;		
		
		//get vars
		if( defined('_JEXEC') ){
			//joomla 1.5
			//$value = JRequest::getVar($value_name,'','post','string', JREQUEST_ALLOWHTML );	
			$value = JRequest::getVar($value_name,'','post','string', JREQUEST_ALLOWRAW);
			$value = addslashes($value);
		}else{
			//joomla 1.0.x
			//$value = mosGetParam( $_POST, $value_name,'', _MOS_ALLOWHTML);
			$value = mosGetParam( $_POST, $value_name,'', _MOS_ALLOWRAW);
		}
		
		$value = str_replace('<br>','<br />', $value);			
		//$value = 'soep';
		return $value;
		
	}	
}

//CHECKBOX
class class_fieldtype_checkbox extends class_pi{	

	function display_config_form($plugin, $type_id, $name, $field_params, $field_id){	
		if(!$field_id){
			//new field, set defaults here
			$field_params['values'] = '';
			$field_params['labels'] = '';
			$field_params['default_value'] = '';
			$field_params['number_checkboxes'] = '';
			
		}

		//description	
		$html = $this->display_field_description($field_params);
		//validation
		$html .= $this->display_field_validation($field_params);
		//validation_mesage
		$html .= $this->display_field_validation_message($field_params);
		//number of checkboxes
		$field_content = '<input type="text" style="width: 20px;" value="'.$field_params['number_checkboxes'].'" name="field_params[number_checkboxes]" />';	
		$html .= $this->display_field(_pi_lang_number_of_checkboxes, $field_content);
		//values
		$field_name = '<span onMouseOver="return overlib(\''._pi_lang_values_tip.'\', CAPTION, \''._pi_lang_values.'\', BELOW, RIGHT);" onMouseOut="return nd();"  class="editlinktip">'._pi_lang_values.'</span>';
		$field_content = '<textarea class="width200" name="field_params[values]" />'.$field_params['values'].'</textarea>';	
		$html .= $this->display_field($field_name, $field_content);
		//default value
		$field_name = _pi_lang_default_value;
		$field_content = '<input type="text" class="width200" value="'.$field_params['default_value'].'" name="field_params[default_value]" /><br />'._pi_lang_default_value_checkbox_info;	
		$html .= $this->display_field($field_name, $field_content);
		//labels
		$field_name = '<span onMouseOver="return overlib(\''._pi_lang_labels_tip.'\', CAPTION, \''._pi_lang_labels.'\', BELOW, RIGHT);" onMouseOut="return nd();"  class="editlinktip">'._pi_lang_labels.'</span>';
		$field_content = '<textarea class="width200" name="field_params[labels]" />'.$field_params['labels'].'</textarea>';	
		$html .= $this->display_field($field_name, $field_content);	
					
		return $html;
	}
	
	function display_item_edit($field, $field_params, $field_values, $field_value, $new_field, $field_id){	
	
		if($new_field){
			//new field	
			$field_value = $field_params['default_value'];
		}		
		$array_checked_boxes = explode('-;-',$field_value);		
		$html = '<div class="field_type_checkbox fieldtype">';
		$html .= '<div class="pi_form_wrapper">';
		$html .= '<div class="pi_width20">';
		$html .= $field->name;
		if($this->check_if_field_param_is_present($field_params, 'validation')){
			if($field_params['validation']){
				$html .= '<img src="'.$this->live_site.'/administrator/components/com_pi_pages_and_items/images/star.gif" alt="required field" />';
			}
		}
		$html .= '</div>';
		$html .= '<div class="pi_width70">';
		if($field_params['description']){
					$html .= '<div>'.$field_params['description'].'</div>';
				}
				
				$labels_array = explode("\n",$field_params['labels']);
				$values_array = explode("\n",$field_params['values']);					
				for($n = 0; $n < count($values_array); $n++){
					$html .= '<label>';				
					$html .= '<input type="checkbox" class="checkbox" value="'.trim($values_array[$n]).'" name="'.$field_id.'[]" id="'.$field_id.'_'.$n.'" ';						
					if(in_array(trim($values_array[$n]), $array_checked_boxes)){
						$html .= ' checked="checked"';
					}
					$html .= ' /> '.trim($labels_array[$n]).'</label><br />';
								
				}
				$html .= '<input type="hidden" value="'.$n.'" id="total_checkboxes_field'.$field_id.'" />';	
				$html .= '<input type="hidden" value="'.$field_params['number_checkboxes'].'" id="min_checkboxes_field'.$field_id.'" />';		
		$html .= '</div>';
		$html .= '</div>';
		$html .= '</div>';		
		
		return $html;
	}
	
	function render_field_output($field, $intro_or_full, $readmore_type=0, $editor_id=0){
		$values = explode('-;-',$field->value);
		/*
		if($field->value==''){			
			if($this->get_field_param($field->params, 'default_value')){
				$temp = $this->get_field_param($field->params, 'default_value');
				$values = explode('-;-',$temp);
			}
		}
		*/
		$html = '';
		if(count($values)==1){
			$html .= $values[0];
		}else{
			$first = 1;
			foreach($values as $value){
				if(!$first){
					$html .= '<br />';
					
				}				
				$html .= $value;
				$first = 0;
			}
		}		
		return $html;
	}	
	
	function field_save($field, $insert_or_update){
		
		$value_name = 'field_values_'.$field->id;
		
		//get vars
		if( defined('_JEXEC') ){
			//joomla 1.5
			$checkboxes = JRequest::getVar($value_name, null, 'post', 'array');		
		}else{
			//joomla 1.0.x
			$checkboxes = mosGetParam( $_POST, $value_name, array(0) );
		}		
		
		$value = implode('-;-',$checkboxes);		
		
		return str_replace('"','&quot;',$value);		
	}
}

//EDITOR
class class_fieldtype_editor extends class_pi{	

	function display_config_form($plugin, $type_id, $name, $field_params, $field_id){
		if(!$field_id){
			//new field, set defaults here
			$field_params['show_field_name'] = 1;	
			$field_params['default_value'] = '';
			$field_params['display_plugin_buttons'] = 1;
			$field_params['height'] = '500';
		}
		//description	
		$html = $this->display_field_description($field_params);
		//show field name
		$field_name = _pi_lang_show_field_name;
		$field_content = '<input type="checkbox" ';
		if($this->check_if_field_param_is_present($field_params, 'show_field_name')){
			if($field_params['show_field_name']){
				$field_content .= ' checked="checked"';
			} 
		}
		$field_content .= 'name="field_params[show_field_name]" value="1" />';	
		$html .= $this->display_field($field_name, $field_content);	
		//display_plugin_buttons
		$field_name = _pi_lang_display_plugin_buttons;
		$field_content = '<input type="checkbox" ';
		if($this->check_if_field_param_is_present($field_params, 'display_plugin_buttons')){
			if($field_params['display_plugin_buttons']){
				$field_content .= ' checked="checked"';
			} 
		}
		$field_content .= 'name="field_params[display_plugin_buttons]" value="1" />';	
		$html .= $this->display_field($field_name, $field_content);	
		//height
		$field_name = _pi_lang_height;
		$field_content = '<input type="text" name="field_params[height]" value="';
		$field_content .= $field_params['height'];
		$field_content .= '" /> ('.strtolower(_pi_lang_alias_info).')';	
		$html .= $this->display_field($field_name, $field_content);			
		//default value
		$field_name = _pi_lang_default_value;		
		$field_content = '<textarea cols="100" rows="25"  name="default_value" >'.$field_params['default_value'].'</textarea>';	
		$html .= $this->display_field($field_name, $field_content);		
		return $html;
	}
	
	
	function params_save($params_string){		
		if(defined('_JEXEC')){
			//joomla 1.5
			$default_value = JRequest::getVar('default_value','','post','string', JREQUEST_ALLOWRAW);							
		}else{
			//joomla 1.0.x					
			$default_value = mosGetParam( $_POST, 'default_value','', _MOS_ALLOWRAW);
			$default_value = stripslashes($default_value);
		}		
		$default_value = str_replace('<br>','<br />', $default_value);
		$default_value = 'default_value-=-'.$default_value.'[;-)# ]';
		$default_temp = 'default_value-=-'.$this->get_field_param($params_string, 'default_value').'[;-)# ]';
		if(strpos($params_string, 'default_value-=-')){
			$params_string = str_replace($default_temp, $default_value, $params_string);			
		}else{
			$params_string .= $default_value;			
		}		
		return $params_string;
	}	
	
	function display_item_edit($field, $field_params, $field_values, $field_value, $new_field, $field_id){	
		$html = '';
		//if new field, set defaults
		if($new_field){			
			$field_value = $field_params['default_value'];
		}
		
		//$html .= $field_id;
		$html .= '<div class="field_type_editor fieldtype">';
		if($this->check_if_field_param_is_present($field_params, 'show_field_name')){
			if($field_params['show_field_name']=='1'){
				$html .= '<div>'.$field->name.'</div>';
			}
		}
		if($this->check_if_field_param_is_present($field_params, 'description')){		
			if($field_params['description']){
				$html .= '<div>'.$field_params['description'].'</div>';
			}
		}
		//check if buttons should be displayed
		$display_plugin_buttons = false;
		if($this->check_if_field_param_is_present($field_params, 'display_plugin_buttons')){		
			if($field_params['display_plugin_buttons']=='1'){
				$display_plugin_buttons = true;
			}
		}
		$html .= stripslashes($this->pi_config['plugin_syntax_cheatcheat']);
		$field_value = stripslashes($field_value);
		$field_value = htmlspecialchars($field_value);		
		if( defined('_JEXEC') ){
			//joomla 1.5
			$editor =& JFactory::getEditor();			
			$html .= $editor->display($field_id,  $field_value , '80%', $field_params['height'], '75', '20', $display_plugin_buttons);			
		}else{
			//joomla 1.0.x
			$html .= '<textarea name="'.$field_id.'" cols="75" rows="10"  mce_editable="true">'.$field_value.'</textarea>';
		}
		$html .= '<div style="clear: both; height: 5px; line-height: 1px;">&nbsp;</div>';
		$html .= '</div>';			
		return $html;
	}
	
	function render_field_output($field, $intro_or_full, $readmore_type=0, $editor_id=0){
		$html = $field->value;
		
		
		$length = strlen($html);
		$pos = strpos($html, '<hr id="system-readmore" />');
		$pos3 = strpos($html, '<hr id=\"system-readmore\" />');					
		if($pos){
			$pos2 = $pos + 27;		
			$introtext = substr($html, 0, $pos);
			$fulltext = substr($html, $pos2, $length);	
		}elseif($pos3){
			$pos4 = $pos3 + 29;		
			$introtext = substr($html, 0, $pos3);
			
			$fulltext = substr($html, $pos4, $length);	
		}else{
			$introtext = $html;
			$fulltext = $html;
		}
		if($intro_or_full=='intro'){
			$html = $introtext;
		}else{
			$html = $fulltext;
		}			
		return $html;		
	}
	
	function field_save($field, $insert_or_update){		
		
		$value_name = 'field_values_'.$field->id;
		
		//get vars
		if( defined('_JEXEC') ){
			//joomla 1.5
			$value = addslashes(JRequest::getVar($value_name,'','post','string', JREQUEST_ALLOWRAW ));	
		}else{
			//joomla 1.0.x
			$value = mosGetParam( $_POST, $value_name,'', _MOS_ALLOWRAW);
		}		
		
		$value = str_replace('<br>','<br />', $value);	
				
		return addslashes($value);		
	}	
}

//IMAGE
class class_fieldtype_image extends class_pi{	

	function display_config_form($plugin, $type_id, $name, $field_params, $field_id){
		if(!$field_id){
			//new field, set defaults here
			$field_params['show_field_name'] = 0;	
			$field_params['resize'] = 0;
			$field_params['max_width'] = '';
			$field_params['max_height'] = '';
			$field_params['delete_old_image'] = 0;
			$field_params['show_src'] = 0;
			$field_params['class_name'] = '';	
			$field_params['only_source'] = 0;
			$field_params['image_dir'] = 'images/stories/';	
			$field_params['auto_alt'] = 1;					
		}
		
		//description	
		$html = $this->display_field_description($field_params);
		//show field name
		$field_name = _pi_lang_show_field_name;
		$field_content = '<input type="checkbox" ';		
		if($this->check_if_field_param_is_present($field_params, 'show_field_name')){
			$field_content .= ' checked="checked"';
		} 
		$field_content .= 'name="field_params[show_field_name]" value="1" />';	
		$html .= $this->display_field($field_name, $field_content);	
		//validation
		$field_name = _pi_lang_validation;
		$field_content = '<input type="checkbox" class="checkbox" ';				
		if($this->check_if_field_param_is_present($field_params, 'validation')){
			if($field_params['validation']){
				$field_content .= ' checked="checked"';
			}
		} 
		$field_content .= 'name="field_params[validation]" value="not_empty" /> '._pi_lang_need_image;	
		$html .= $this->display_field($field_name, $field_content);	
		//validation_mesage
		$html .= $this->display_field_validation_message($field_params);
		//resize
		$field_name = _pi_lang_resize;
		$field_content = '<input type="checkbox" class="checkbox" ';		
		if($this->check_if_field_param_is_present($field_params, 'resize')){
			if($field_params['resize']){
				$field_content .= ' checked="checked"';
			}
		} 
		$field_content .= 'name="field_params[resize]" value="1" />';	
		$html .= $this->display_field($field_name, $field_content);		
		//width
		$field_name = _pi_lang_max_width;
		$field_content = '<input type="text" class="width200" value="'.$field_params['max_width'].'" name="field_params[max_width]" />';	
		$html .= $this->display_field($field_name, $field_content);	
		//height
		$field_name = _pi_lang_max_height;
		$field_content = '<input type="text" class="width200" value="'.$field_params['max_height'].'" name="field_params[max_height]" />';	
		$html .= $this->display_field($field_name, $field_content);
		//old image
		$field_name = _pi_lang_delete_old_image;
		$field_content = '<input type="checkbox" class="checkbox" ';		
		if($this->check_if_field_param_is_present($field_params, 'delete_old_image')){
			$field_content .= ' checked="checked"';
		} 
		$field_content .= 'name="field_params[delete_old_image]" value="1" />';	
		$html .= $this->display_field($field_name, $field_content);	
		//show source
		$field_name = _pi_lang_show_src;
		$field_content = '<input type="checkbox" class="checkbox" ';		
		if($this->check_if_field_param_is_present($field_params, 'show_src')){
			if($field_params['show_src']){
				$field_content .= ' checked="checked"';
			}
		} 
		$field_content .= 'name="field_params[show_src]" value="1" />';	
		$html .= $this->display_field($field_name, $field_content);	
		//classname
		$field_name = _pi_lang_class_name;
		$field_content = '<input type="text" class="width200" value="'.$field_params['class_name'].'" name="field_params[class_name]" /> '._pi_lang_class_name2;	
		$html .= $this->display_field($field_name, $field_content);	
		//output_only_source
		$field_name = _pi_lang_output_only_source;
		$field_content = '<input type="checkbox" class="checkbox" ';
		if($this->check_if_field_param_is_present($field_params, 'only_source')){
			if($field_params['only_source']){
				$field_content .= ' checked="checked"';
			}
		} 
		$field_content .= 'name="field_params[only_source]" value="1" />';	
		$html .= $this->display_field($field_name, $field_content);	
		//auto add image source to alt field
		$field_name = _pi_lang_auto_add_src_to_alt;
		$field_content = '<input type="checkbox" class="checkbox" ';
		if($this->check_if_field_param_is_present($field_params, 'auto_alt')){
			if($field_params['auto_alt']){
				$field_content .= ' checked="checked"';
			}
		} 
		$field_content .= 'name="field_params[auto_alt]" value="1" /> '._pi_lang_auto_add_src_to_alt2;	
		$html .= $this->display_field($field_name, $field_content);	
		//image dit
		$field_name = _pi_lang_image_dir;
		$field_content = '<input type="text" class="width200" value="'.$field_params['image_dir'].'" name="field_params[image_dir]" /> '._pi_lang_image_dir_example;	
		$html .= $this->display_field($field_name, $field_content);	
		return $html;
	}
	
	function display_item_edit($field, $field_params, $field_values, $field_value, $new_field, $field_id){	
		if($new_field){
			//new field		
			$src = '';
			$alt = '';
		}else{
			//edit field			
			if($this->check_if_field_param_is_present($field_values, 'src')){	
				$src = $field_values['src'];
			}else{
				$src = 0;
			}
			if($this->check_if_field_param_is_present($field_values, 'alt')){				
				$alt = $field_values['alt'];
			}else{				
				$alt = '';
			}
		}
				
		$html = '<div class="field_type_image fieldtype">';
	
		if($this->check_if_field_param_is_present($field_params, 'show_field_name')){		
			$html .= '<div>'.$field->name.'</div>';
		}
		if($field_params['description']){
			$html .= '<div>'.$field_params['description'].'</div>';
		} 
		
		$html .= '<div class="pi_form_wrapper">';
		$html .= '<div class="pi_width20">';
		$html .= '<span onMouseOver="return overlib(\''._pi_lang_image_tip.'\', CAPTION, \''._pi_lang_image.'\', BELOW, RIGHT);" onMouseOut="return nd();"  class="editlinktip">';
		$html .= _pi_lang_image;
		$html .= '</span>';		
		if($this->check_if_field_param_is_present($field_params, 'validation')){
			$html .= '<img src="'.$this->live_site.'/administrator/components/com_pi_pages_and_items/images/star.gif" alt="required field" />';
		}
		$html .= '</div>';
		$html .= '<div class="pi_width70">';
		
		$html .= '<script language="javascript"  type="text/javascript">'."\n";
		//function to check extension
		$html .= 'function check_extension_'.$field_id.'(thisthing){'."\n";		
		$html .= 'value = thisthing.value.toLowerCase();'."\n";	
		$html .= 'pos_jpg = value.indexOf(".jpg");'."\n";
		$html .= 'pos_jpeg = value.indexOf(".jpeg");'."\n";
		$html .= 'pos_gif = value.indexOf(".gif");'."\n";
		$html .= 'pos_png = value.indexOf(".png");'."\n";		
		$html .= 'if(pos_jpg==-1 && pos_jpeg==-1 && pos_gif==-1 && pos_png==-1){'."\n";
		$html .= 'thisthing.value = \'\';'."\n";		
		$html .= 'alert(\'wrong file-type. allowed are: gif, jpg, png and jpeg\')'."\n";		
		$html .= '}'."\n";
		$html .= '}'."\n";
		$html .= '</script>'."\n";	
		
		$html .= '<input type="file" value="1" name="'.$field_id.'_image" id="'.$field_id.'_image" onchange="check_extension_'.$field_id.'(this);" />';
		$html .= '</div>';
		$html .= '</div>';
		if($src){
			$html .= '<div class="pi_form_wrapper">';
			$html .= '<div class="pi_width20">';
			$html .= '&nbsp;';
			$html .= '</div>';
			$html .= '<div class="pi_width70">';
			$html .= '<div style="width: 400px; overflow-x: auto; overflow-y: hidden;">';
			//$html .= '<img src="../images/stories/'.$src.'" alt="'.$src.'" />';	
			$html .= '<img src="'.$this->live_site.$field_params['image_dir'].$src.'" alt="'.$src.'" />';			
			$html .= '</div>';
			$html .= '</div>';
			$html .= '</div>';
			$html .= '<div class="pi_form_wrapper">';
			$html .= '<div class="pi_width20">';
			$html .= _pi_lang_delete_image;
			$html .= ':</div>';
			$html .= '<div class="pi_width70">';
			$html .= '<input type="checkbox" class="checkbox" name="'.$field_id.'_delete_image" value="1" />';
			$html .= '</div>';
			$html .= '</div>';				
			if($this->check_if_field_param_is_present($field_params, 'show_src')){							
				$html .= '<div class="pi_form_wrapper">';
				$html .= '<div class="pi_width20">';
				$html .= _pi_lang_src;
				$html .= ':</div>';
				$html .= '<div class="pi_width70">';
				$html .= '<input type="text" class="width200" value="';
				$html .= $field_params['image_dir'];
				$html .= $field_values['src'].'" name="'.$field_id.'_src" id="'.$field_id.'_src" />';								
				$html .= '</div>';
				$html .= '</div>';
			}else{
				$html .= '<input type="hidden" class="width200" value="';
				$html .= $field_params['image_dir'];
				$html .= $field_values['src'].'" name="'.$field_id.'_src" id="'.$field_id.'_src" />';	
			}
		}else{
			echo '<input type="hidden" class="width200" value=""  name="'.$field_id.'_src" id="'.$field_id.'_src" />';
		}
		$html .= '<div class="pi_form_wrapper">';
		$html .= '<div class="pi_width20">';
		$html .= _pi_lang_alt_text;
		$html .= ':</div>';
		$html .= '<div class="pi_width70">';
		$html .= '<input type="text" class="width200" value="'.$alt.'" name="'.$field_id.'_alt" />';				
		$html .= '</div>';
		$html .= '</div>';
		$html .= '</div>';
		$html .= '<input type="hidden" name="'.$field_id.'_max_width" value="'.$field_params['max_width'].'" />';
		$html .= '<input type="hidden" name="'.$field_id.'_max_height" value="'.$field_params['max_height'].'" />';
		$html .= '<input type="hidden" name="'.$field_id.'_delete_old_image" value="';
		if($this->check_if_field_param_is_present($field_params, 'delete_old_image')){
			$html .= '1';
		}
		$html .=  '" />';
		$html .= '<input type="hidden" name="'.$field_id.'_resize" value="';		
		if($this->check_if_field_param_is_present($field_params, 'resize')){
			$html .= '1';
		}
		$html .=  '" />';	
		$html .= '<input type="hidden" name="'.$field_id.'_auto_alt" value="';		
		if($this->check_if_field_param_is_present($field_params, 'auto_alt')){
			$html .= '1';
		}
		$html .=  '" />';	
		$html .= '<input type="hidden" name="'.$field_id.'_image_dir" value="';		
		if($this->check_if_field_param_is_present($field_params, 'image_dir')){
			$html .= $field_params['image_dir'];
		}
		$html .=  '" />';			
		return $html;
	}
	
	function render_field_output($field, $intro_or_full, $readmore_type=0, $editor_id=0){
		$html = '';
		if($this->get_field_value($field->value, 'src')!=''){
			if($this->get_field_param($field->params, 'only_source')){
				$html .= $this->get_field_value($field->value, 'src');
			}else{
				$html .= '<img src="';
				$html .= $this->get_field_param($field->params, 'image_dir');
				$html .= $this->get_field_value($field->value, 'src').'"';
				$html .= ' alt="'.addslashes($this->get_field_value($field->value, "alt")).'"';
				if($this->get_field_param($field->params, 'class_name')!=''){
					$html .= ' class="'.$this->get_field_param($field->params, 'class_name').'"';
				}	
				$html .= ' />';
			}
		}		
		return $html;
	}
	
	function field_save($field, $insert_or_update){		
		
		$value_name = 'field_values_'.$field->id;		
		
		$image = $value_name.'_image';
		$max_width = $this->get_var($value_name.'_max_width');
		$max_height = $this->get_var($value_name.'_max_height');
		$delete_old_image = $this->get_var($value_name.'_delete_old_image');
		$resize = $this->get_var($value_name.'_resize');
		$src = $this->get_var($value_name.'_src', false);
		$delete_image = $this->get_var($value_name.'_delete_image', false);
		$image_dir = $this->get_var($value_name.'_image_dir', false);
		$auto_alt = $this->get_var($value_name.'_auto_alt', false);
		
		$image_upload = false;
		
		//check if there is an image to upload	
		$userfile_name = strtolower($_FILES[$image]['name']);	
		if($userfile_name && !$delete_image){
			$image_upload = true;
		}
		
		//if image edit, delete old image
		if(($userfile_name && $src && $delete_old_image) || $delete_image){
			//get the bare src
			$source_bits = explode('/',$src);
			$source_bits = array_reverse($source_bits);
			$src_delete = $source_bits[0];		
			if(file_exists(dirname(__FILE__).'/../../../'.$image_dir.$src_delete)){
				unlink(dirname(__FILE__).'/../../../'.$image_dir.$src_delete);	
			}						
		}
		
		$new_name = '';
		
		//upload image		
		if($image_upload==true){		
			
			$userfile_name = strtolower($_FILES[$image]['name']);			
			$userfile_tmp = $_FILES[$image]['tmp_name'];
			$userfile_size = $_FILES[$image]['size'];
			$userfile_type = $_FILES[$image]['type'];
			
			if (isset($_FILES[$image]['name'])){
				
				// get extension
				$extension = explode(".", $userfile_name);
				$last = count($extension) - 1;
				$extension = "$extension[$last]";
				
				//check extension
				$allowed_ext = "jpg jpeg gif png";			
				$allowed_extensions = explode(" ", $allowed_ext);			
				if (!in_array($extension, $allowed_extensions)){
					die('wrong file-type. allowed are: '.$allowed_ext);
				}			
				
				//rewrite jpeg to jpg
				if($extension=='jpeg'){
					$extension = 'jpg';
				}						   			
	
				// get the old name of the file	
				$old_name = str_replace('.'.$extension,'',$userfile_name);					
				
				//rename file if already exist
				if(file_exists(dirname(__FILE__).'/../../../'.$image_dir.$old_name.'.'.$extension)){			
					$j = 2;
					while (file_exists(dirname(__FILE__).'/../../../'.$image_dir.$old_name.'-'.$j.".".$extension)){
						$j = $j + 1;
					}
					$new_name = $old_name . "-" . $j;	
				}else{
					$new_name = $old_name;
				}
				
				//replace spaces by underscores
				$new_name = str_replace(' ', '_', $new_name);			
				
				//upload image
				$prod_img = dirname(__FILE__).'/../../../'.$image_dir.$new_name.'.'.$extension;
				//exit($prod_img);					
				if(!move_uploaded_file($userfile_tmp, $prod_img)){
					//die('Problem uploading image. File is too big. Try making the image smaller with an image-editor (like Photoshop or Gimp) and try again.');
				}			
				
				//get sizes and ratio
				$sizes = getimagesize($prod_img);	
				$aspect_ratio = $sizes[1]/$sizes[0]; 
				
				
				
				//resize uploaded image  
				if (($sizes[0] > $max_width || $sizes[1] > $max_height) && $resize){
					
					$widthratio = $sizes[0]/$max_width; 			
					$heightratio = $sizes[1]/$max_height; 			
		
					$imgnewwidth = $max_width; 
					$imgnewheight = $max_height; 
		
					if($widthratio <= $heightratio){ 
						$imgnewwidth = $sizes[0]/$heightratio; 					
						$newwidth = round($imgnewwidth); 
						$newheight = round($imgnewheight); 					
						 
					}else{ 
						$imgnewheight = $sizes[1]/$widthratio;						
						$newwidth = round($imgnewwidth); 
						$newheight = round($imgnewheight); 					
					}
					
					//ini_set('memory_limit', '120M');
					$imgnew = imagecreatetruecolor($newwidth,$newheight);				
					if($extension=='jpg'){
						$srcimg=imagecreatefromjpeg($prod_img);
					}elseif($extension=='gif'){
						$srcimg=imagecreatefromgif($prod_img);
					}elseif($extension=='png'){
						$srcimg=imagecreatefrompng($prod_img);
					}
					if(function_exists('imagecopyresampled')){
						imagecopyresampled($imgnew,$srcimg,0,0,0,0,$newwidth,$newheight,ImageSX($srcimg),ImageSY($srcimg));
					}else{
						imagecopyresized($imgnew,$srcimg,0,0,0,0,$newwidth,$newheight,ImageSX($srcimg),ImageSY($srcimg));
					}				
					if($extension=='jpg'){
						imagejpeg($imgnew,$prod_img,90);
					}elseif($extension=='gif'){
						imagegif($imgnew,$prod_img);
					}elseif($extension=='png'){
						imagepng($imgnew,$prod_img);
					}	
				}
			}
			$src = $new_name.'.'.$extension;			
		}else{
			//nothing has been uploaded
			
			$source_bits = explode('/',$src);
			$source_bits = array_reverse($source_bits);
			$src = $source_bits[0];		
			
		}
		
		$alt = addslashes($this->get_var($value_name.'_alt'));		
		if($auto_alt && $alt==''){
			$alt = $new_name;
		}		
		$value = '';
		if($src){		
			$value = 'src-=-'.$src.'[;-)# ]alt-=-'.$alt.'[;-)# ]';
		}
		if($delete_image){
			$value = '';
		}
		return $value;	
	}	
	
	function item_delete($item_id, $type_id, $field){	
		
		$field_id = $field->id;			
		
		//get value of image
		$this->db->setQuery("SELECT value "
		."FROM #__pi_custom_fields_values "
		."WHERE (field_id='$field_id' AND item_id='$item_id') "
		."LIMIT 1 "
		);
		$images_rows = $this->db->loadObjectList();
		foreach($images_rows as $image_row){	
			$image_value = $image_row->value;
		}
		$value_array = explode('[;-)#',$image_value);
		$src_part = $value_array[0];
		$src_array = explode('=-',$src_part);
		$src = $src_array[1];
		
		//get image dir
		$image_dir = $this->get_field_param($field->params, 'image_dir');		
		
		//delete image
		if(file_exists(dirname(__FILE__).'/../../../'.$image_dir.$src)){			
			unlink(dirname(__FILE__).'/../../../'.$image_dir.$src);
		}
	}	
	
	function field_delete($field){
	
		$field_id = $field->id;			
		
		//get values of images
		$this->db->setQuery("SELECT value "
		."FROM #__pi_custom_fields_values "
		."WHERE field_id='$field_id' "
		);
		$images_rows = $this->db->loadObjectList();
		foreach($images_rows as $image_row){	
			$image_value = $image_row->value;
			$value_array = explode('[;-)#',$image_value);
			$src_part = $value_array[0];
			$src_array = explode('=-',$src_part);
			$src = $src_array[1];
			
			//get image dir
			$image_dir = $this->get_field_param($field->params, 'image_dir');			
			
			//delete image
			if(file_exists(dirname(__FILE__).'/../../../'.$image_dir.$src)){			
				unlink(dirname(__FILE__).'/../../../'.$image_dir.$src);
			}
		}				
	}
}


//ITEM_AUTHOR
class class_fieldtype_item_author extends class_pi{	

	function params_base()
	{
		$param[] = 'only_once=1';
		$param[] = 'no_pi_fish_table=1';
		return $param;
	}
	
	function display_config_form($plugin, $type_id, $name, $field_params, $field_id){
		if(!$field_id){
			//new field, set defaults here
			$field_params['name_type'] = 'username';
		}
		//display
		$field_name = _pi_lang_display;
		$field_content = '<label><input type="radio" class="radio" value="username" name="field_params[name_type]"';
		if($field_params['name_type']=='username'){
			$field_content .= 'checked="checked"';
		} 
		$field_content .= ' />';
		$field_content .= _pi_lang_username;
		$field_content .= '</label><br />';
		$field_content .= '<label><input type="radio" class="radio" value="name" name="field_params[name_type]" ';
		if($field_params['name_type']=='name'){
			$field_content .= 'checked="checked"';
		} 
		$field_content .= ' />';
		$field_content .= _pi_lang_name;
		$field_content .= '</label>';	
		$html = $this->display_field($field_name, $field_content);
		//info
		$html .= '<div class="pi_form_wrapper">';			
		$html .= _pi_lang_not_show_on_edit.' '._pi_lang_item_author_info; 		
		$html .= '</div>';		
		return $html;
	}
	
	function display_item_edit($field, $field_params, $field_values, $field_value, $new_field, $field_id){	
		return '';
	}
	
	function render_field_output($field, $intro_or_full, $readmore_type=0, $editor_id=0){
	
		$item_id = $field->item_id;
		$this->db->setQuery("SELECT created_by, created_by_alias FROM #__content WHERE id='$item_id' LIMIT 1");
		$item_authors = $this->db->loadObjectList();		
		$author_id = 0;		
		foreach($item_authors as $item_author){
			
			$author_alias = $item_author->created_by_alias;	
			if($author_alias){
				$author_name = $author_alias;
			}else{
				$author_id = $item_author->created_by;
				$this->db->setQuery("SELECT name, username FROM #__users WHERE id='$author_id' LIMIT 1");
				$authors = $this->db->loadObjectList();
				$author = $authors[0];	
				if($this->get_field_param($field->params,'name_type')=='username'){
					$author_name = $author->username;
				}else{
					$author_name = $author->name;
				}	
			}
		}			
			
		return $author_name;
	}
}

//ITEM_CREATION_DATE
class class_fieldtype_item_creation_date extends class_pi{	

	function params_base()
	{
		$param[] = 'only_once=1';
		$param[] = 'no_pi_fish_table=1';
		return $param;
	}
	
	function display_config_form($plugin, $type_id, $name, $field_params, $field_id){
		if(!$field_id){
			//new field, set defaults here
			$field_params['date_format'] = 'd F Y H:i:s';
		}

		//display
		$field_name = _pi_lang_date_format;
		$field_content = '<input type="text" class="width200" value="';
		$field_content .= $field_params['date_format'];
		$field_content .= '" name="field_params[date_format]" />';
		$field_content .= '<br />';
		$field_content .= _pi_lang_example; 
		$field_content .= '<br />d F Y H:i:s <br /><a href="http://php.net/manual/en/function.date.php" target="_blank">';
		$field_content .= 'http://php.net/manual/en/function.date.php</a><br />';
		$field_content .= _pi_lang_not_show_on_edit;
		$html = $this->display_field($field_name, $field_content);
		
		return $html;
	}
	
	function display_item_edit($field, $field_params, $field_values, $field_value, $new_field, $field_id){	
		return '';
	}
	
	function render_field_output($field, $intro_or_full, $readmore_type=0, $editor_id=0){
		
		$item_id = $field->item_id;
		
		$this->db->setQuery("SELECT created FROM #__content WHERE id='$item_id' LIMIT 1");
		$items = $this->db->loadResultArray();
		$item = $items[0];	
		
		$date = strtotime($item);
		$formatted_date = date($this->get_field_param($field->params, 'date_format'), $date);
		//$formatted_date = JHTML::_('date', $date, JText::_('DATE_FORMAT_LC1'));
		//$formatted_date = date(JText::_('DATE_FORMAT_LC1'), $date);
		return $formatted_date;
		//setlocale(LC_TIME, "nl_NL");
		//$test = strftime(" in Nederlands %A.\n");
		//return $test;
	}
}

//ITEM_MODIFIED_DATE
class class_fieldtype_item_modified_date extends class_pi{	

	function params_base()
	{
		$param[] = 'only_once=1';
		$param[] = 'no_pi_fish_table=1';
		return $param;
	}
	
	function display_config_form($plugin, $type_id, $name, $field_params, $field_id){
		if(!$field_id){
			//new field, set defaults here
			$field_params['date_format'] = 'd F Y H:i:s';
		}

		//display
		$field_name = _pi_lang_date_format;
		$field_content = '<input type="text" class="width200" value="';
		$field_content .= $field_params['date_format'];
		$field_content .= '" name="field_params[date_format]" />';
		$field_content .= '<br />';
		$field_content .= _pi_lang_example; 
		$field_content .= '<br />d F Y H:i:s <br /><a href="http://php.net/manual/en/function.date.php" target="_blank">';
		$field_content .= 'http://php.net/manual/en/function.date.php</a><br />';
		$field_content .= _pi_lang_not_show_on_edit;
		$html = $this->display_field($field_name, $field_content);
		
		return $html;
	}
	
	function display_item_edit($field, $field_params, $field_values, $field_value, $new_field, $field_id){	
		return '';
	}
	
	function render_field_output($field, $intro_or_full, $readmore_type=0, $editor_id=0){
		
		$item_id = $field->item_id;
		
		$this->db->setQuery("SELECT modified FROM #__content WHERE id='$item_id' LIMIT 1");
		$items = $this->db->loadResultArray();
		$modified = $items[0];
		
		$date = strtotime($modified);
		$formatted_date = date($this->get_field_param($field->params, 'date_format'), $date);
		
		return $formatted_date;
	}
}

//ITEM_PUBLISH_DATE
class class_fieldtype_item_publish_date extends class_pi{	

	function params_base()
	{
		$param[] = 'only_once=1';
		$param[] = 'no_pi_fish_table=1';
		return $param;
	}
	
	function display_config_form($plugin, $type_id, $name, $field_params, $field_id){
		if(!$field_id){
			//new field, set defaults here
			$field_params['date_format'] = 'd F Y H:i:s';
		}

		//display
		$field_name = _pi_lang_date_format;
		$field_content = '<input type="text" class="width200" value="';
		$field_content .= $field_params['date_format'];
		$field_content .= '" name="field_params[date_format]" />';
		$field_content .= '<br />';
		$field_content .= _pi_lang_example; 
		$field_content .= '<br />d F Y H:i:s <br /><a href="http://php.net/manual/en/function.date.php" target="_blank">';
		$field_content .= 'http://php.net/manual/en/function.date.php</a><br />';
		$field_content .= _pi_lang_not_show_on_edit;
		$html = $this->display_field($field_name, $field_content);
		
		return $html;
	}
	
	function display_item_edit($field, $field_params, $field_values, $field_value, $new_field, $field_id){	
		return '';
	}
	
	function render_field_output($field, $intro_or_full, $readmore_type=0, $editor_id=0){
		
		$item_id = $field->item_id;
		
		$this->db->setQuery("SELECT publish_up FROM #__content WHERE id='$item_id' LIMIT 1");
		$items = $this->db->loadResultArray();
		$publish_up = $items[0];	
		
		$date = strtotime($publish_up);
		$formatted_date = date($this->get_field_param($field->params, 'date_format'), $date);
		
		return $formatted_date;
	}
}

//ITEM_TITLE
class class_fieldtype_item_title extends class_pi{	

	function params_base()
	{
		$param[] = 'only_once=1';
		$param[] = 'no_pi_fish_table=1';
		return $param;
	}

	function display_config_form($plugin, $type_id, $name, $field_params, $field_id){
		$html = '<div class="pi_form_wrapper">';				
		$html .= _pi_lang_not_show_on_edit.' '._pi_lang_item_title_info; 
		$html .= '</div>';		
		return $html;
	}
	
	function display_item_edit($field, $field_params, $field_values, $field_value, $new_field, $field_id){	
		return '';
	}
	
	function render_field_output($field, $intro_or_full, $readmore_type=0, $editor_id=0){
		
		$item_id = $field->item_id;
		
		$this->db->setQuery("SELECT title FROM #__content WHERE id='$item_id' LIMIT 1");
		$items = $this->db->loadResultArray();
		$title = $items[0];					
		return addslashes($title);
	}
}

//ITEM_VERSION
class class_fieldtype_item_version extends class_pi{	

	function params_base()
	{
		$param[] = 'only_once=1';
		$param[] = 'no_pi_fish_table=1';
		return $param;
	}
	
	function display_config_form($plugin, $type_id, $name, $field_params, $field_id){
		$html = '<div class="pi_form_wrapper">';				
		$html .= _pi_lang_not_show_on_edit.' '._pi_lang_item_version_info; 
		$html .= '</div>';		
		return $html;
	}
	
	function display_item_edit($field, $field_params, $field_values, $field_value, $new_field, $field_id){	
		return '';
	}
	
	function render_field_output($field, $intro_or_full, $readmore_type=0, $editor_id=0){
		
		$item_id = $field->item_id;
		
		$this->db->setQuery("SELECT version FROM #__content WHERE id='$item_id' LIMIT 1");
		$items = $this->db->loadObjectList();
		$item = $items[0];					
		return $item->version;
	}
}

//RADIO
class class_fieldtype_radio extends class_pi{	

	function display_config_form($plugin, $type_id, $name, $field_params, $field_id){
		if(!$field_id){
			//new field, set defaults here
			$field_params['values'] = '';
			$field_params['labels'] = '';
			$field_params['default_value'] = '';
			$field_params['validation'] = '';
		}
		$html = '';
		//show field name
		$field_name = _pi_lang_show_field_name;
		$field_content = '<input type="checkbox" ';
		if($this->check_if_field_param_is_present($field_params, 'show_field_name')){
			if($field_params['show_field_name']){
				$field_content .= ' checked="checked"';
			} 
		}
		$field_content .= 'name="field_params[show_field_name]" value="1" />';	
		$html .= $this->display_field($field_name, $field_content);			
		//description	
		$html .= $this->display_field_description($field_params);
		//validation
		$html .= $this->display_field_validation($field_params);
		//validation_mesage
		$html .= $this->display_field_validation_message($field_params);
		//values
		$field_name = '<span onMouseOver="return overlib(\''._pi_lang_values_tip.'\', CAPTION, \''._pi_lang_values.'\', BELOW, RIGHT);" onMouseOut="return nd();"  class="editlinktip">'._pi_lang_values.'</span>';
		$field_content = '<textarea class="width200" name="field_params[values]" />'.$field_params['values'].'</textarea>';	
		$html .= $this->display_field($field_name, $field_content);		
		//default value
		$html .= $this->display_field_default_value($field_params);
		//labels
		$field_name = '<span onMouseOver="return overlib(\''._pi_lang_labels_tip.'\', CAPTION, \''._pi_lang_labels.'\', BELOW, RIGHT);" onMouseOut="return nd();"  class="editlinktip">'._pi_lang_labels.'</span>';
		$field_content = '<textarea class="width200" name="field_params[labels]" />'.$field_params['labels'].'</textarea>';	
		$html .= $this->display_field($field_name, $field_content);		
		return $html;
	}
	
	function display_item_edit($field, $field_params, $field_values, $field_value, $new_field, $field_id){	
		if($new_field){
			//new field		
			$field_value = $field_params['default_value'];
		}
		$html = '<div class="field_type_radio fieldtype">';
		$field_name = '';
		if($this->check_if_field_param_is_present($field_params, 'show_field_name')){
			if($field_params['show_field_name']=='1'){
				$field_name .= $field->name;
			}
		}
		if($this->check_if_field_param_is_present($field_params, 'validation')){	
			if($field_params['validation']){
				$field_name .= '<img src="'.$this->live_site.'/administrator/components/com_pi_pages_and_items/images/star.gif" alt="required field" />';
			}	
		}
		$field_content = '';
		if($field_params['description']){
			$field_content .= '<div>'.$field_params['description'].'</div>';
		}
					
		$labels_array = explode("\n",$field_params['labels']);
		$values_array = explode("\n",$field_params['values']);							
		for($n = 0; $n < count($values_array); $n++){
			$field_content .= '<label>';
			$field_content .= '<input type="radio" class="radio" value="'.trim($values_array[$n]).'" name="'.$field_id.'[]" id="'.$field_id.'_'.$n.'"';
			if(trim($values_array[$n])==$field_value){
				$field_content .= ' checked="checked"';
			}
			$field_content .= ' /> '.trim($labels_array[$n]).'</label><br />';
						
		}		
		$html .= $this->display_field($field_name, $field_content);
		$html .= '</div>';
		return $html;
	}
	
	function render_field_output($field, $intro_or_full, $readmore_type=0, $editor_id=0){
		return $field->value;
	}
}

//SELECT
class class_fieldtype_select extends class_pi{	

	function display_config_form($plugin, $type_id, $name, $field_params, $field_id){
		if(!$field_id){
			//new field, set defaults here
			$field_params['values'] = '';
			$field_params['labels'] = '';
			$field_params['default_value'] = '';
			$field_params['multiselect'] = '0';
		}		
		//description	
		$html = $this->display_field_description($field_params);
		//validation
		$html .= $this->display_field_validation($field_params);
		//validation_mesage
		$html .= $this->display_field_validation_message($field_params);
		//values
		$field_name = '<span onMouseOver="return overlib(\''._pi_lang_values_tip.'\', CAPTION, \''._pi_lang_values.'\', BELOW, RIGHT);" onMouseOut="return nd();"  class="editlinktip">'._pi_lang_values.'</span>';
		$field_content = '<textarea class="width200" name="field_params[values]" />'.$field_params['values'].'</textarea>';	
		$html .= $this->display_field($field_name, $field_content);		
		//default value
		$field_name = _pi_lang_default_value;
		$field_content = '<input type="text" class="width200" value="'.$field_params['default_value'].'" 

name="field_params[default_value]" /><br />'._pi_lang_default_value_select_info;	
		$html .= $this->display_field($field_name, $field_content);
		//labels
		$field_name = '<span onMouseOver="return overlib(\''._pi_lang_labels_tip.'\', CAPTION, \''._pi_lang_labels.'\', BELOW, RIGHT);" onMouseOut="return nd();"  class="editlinktip">'._pi_lang_labels.'</span>';
		$field_content = '<textarea class="width200" name="field_params[labels]" />'.$field_params['labels'].'</textarea>';	
		$html .= $this->display_field($field_name, $field_content);	
		//multiselect
		$field_name = _pi_lang_multiselect;
		$field_content = '<input type="checkbox" class="checkbox" ';
		if($this->check_if_field_param_is_present($field_params, 'multiselect')){
			if($field_params['multiselect']=='1'){
				$field_content .= ' checked="checked"';
			}
		} 
		$field_content .= 'name="field_params[multiselect]" value="1" />';	
		$html .= $this->display_field($field_name, $field_content);	
		return $html;
	}
	
	function display_item_edit($field, $field_params, $field_values, $field_value, $new_field, $field_id){	
		if($new_field){
			//new field		
			$field_value = $field_params['default_value'];
		}
		
		//if multiselect make values into array to check for displaying selection		
		if($this->check_if_field_param_is_present($field_params, 'multiselect')){			
			$field_value = explode('-;-',$field_value);
		}
		
		$html = '<div class="field_type_select fieldtype">';
		$field_name = $field->name;		
		if($this->check_if_field_param_is_present($field_params, 'validation')){	
			$field_name .= '<img src="'.$this->live_site.'/administrator/components/com_pi_pages_and_items/images/star.gif" alt="required field" />';
		}		
		
		if($this->check_if_field_param_is_present($field_params, 'description')){
			$field_content = '<div>'.$field_params['description'].'</div>';
		}		
		
		if($this->check_if_field_param_is_present($field_params, 'labels')){	
			$labels_array = explode("\n",$field_params['labels']);
		}else{
			$labels_array = array();
		}
		
		if($this->check_if_field_param_is_present($field_params, 'values')){	
			$values_array = explode("\n",$field_params['values']);
		}else{
			$values_array = array();
		}
		
		$field_content .= '<select name="'.$field_id.'[]" id="'.$field_id.'"';
		if($this->check_if_field_param_is_present($field_params, 'multiselect')){	
			$field_content .= ' multiple="multiple"';
		}
		$field_content .= '>';							
		for($n = 0; $n < count($values_array); $n++){
			$values_array[$n] = str_replace('"','&quote;', $values_array[$n]);
			$field_content .= '<option value="'.$values_array[$n].'"';
			if($this->check_if_field_param_is_present($field_params, 'multiselect')){	
				if(in_array(trim($values_array[$n]), $field_value)){
					$field_content .= ' selected="selected"';
				}
			}else{
				if(trim($values_array[$n])==$field_value){
					$field_content .= ' selected="selected"';
				}
			}
			$field_content .= '>'.$labels_array[$n].'</option>';			
		}
		$field_content .= '</select>';	
		$html .= $this->display_field($field_name, $field_content);
		$html .= '</div>';
		return $html;
	}
	
	function field_save($field, $insert_or_update){	
		
		$value_name = 'field_values_'.$field->id;		
		
		//get vars
		if( defined('_JEXEC') ){
			//joomla 1.5
			$checkboxes = JRequest::getVar($value_name, null, 'post', 'array');		
		}else{
			//joomla 1.0.x
			$checkboxes = mosGetParam( $_POST, $value_name, array(0) );
		}		
		
		$value = implode('-;-',$checkboxes);
		$value = addslashes($value);		
		return $value;		
	}
	
	function render_field_output($field, $intro_or_full, $readmore_type=0, $editor_id=0){
		$values = explode('-;-',$field->value);
		$html = '';
		for($n = 0; $n < count($values); $n++){			
			if($this->get_field_param($field->params, 'multiselect') && $n!=0){
				$html .= '<br />';
			}
			$value = addslashes($values[$n]);
			$value = str_replace('&quote;','"',$value);
			$html .= $value;
		}
		return $html;
	}
}

//TEXT
class class_fieldtype_text extends class_pi{	

	function display_config_form($plugin, $type_id, $name, $field_params, $field_id){
		if(!$field_id){
			$field_params['max_chars'] = '';
			$field_params['validation']= '';
		}
		//description	
		$html = $this->display_field_description($field_params);
		//validation
		$field_name = _pi_lang_validation;
		$field_content = '<select name="field_params[validation]">';
		$field_content .= '<option value="0" ';
		if($field_params['validation']==0){
			$field_content .= 'selected="selected"';
		} 
		$field_content .= '>';
		$field_content .= _pi_lang_no_validation;
		$field_content .= '</option>';
		$field_content .= '<option value="not_empty" ';
		if($field_params['validation']=='not_empty'){
			$field_content .= 'selected="selected"';
		} 
		$field_content .= '>';
		$field_content .= _pi_lang_not_empty; 
		$field_content .= '</option>';
		$field_content .= '<option value="emailadress" ';
		if($field_params['validation']=='emailadress'){
			$field_content .= 'selected="selected"';
		} 
		$field_content .= '>';
		$field_content .= _pi_lang_emailadress;
		$field_content .= '</option>';
		$field_content .= '</select>';	
		$html .= $this->display_field($field_name, $field_content);		
		//validation_mesage
		$html .= $this->display_field_validation_message($field_params);
		//max characters
		$field_name = _pi_lang_field_max_chars;
		$field_content = '<input type="text" class="width200" value="'.$field_params['max_chars'].'" name="field_params[max_chars]" />';	
		$html .= $this->display_field($field_name, $field_content);		
		//default value	
		$html .= $this->display_field_default_value($field_params);			
		return $html;
	}
	
	function display_item_edit($field, $field_params, $field_values, $field_value, $new_field, $field_id){	
		if($new_field){
			//new field		
			$field_value = $field_params['default_value'];
		}
		
		$html = '<div class="field_type_text fieldtype">';
		$field_name = $field->name;
		if($field_params['validation']){
			$field_name .= '<img src="'.$this->live_site.'/administrator/components/com_pi_pages_and_items/images/star.gif" alt="required field" />';
		}		
		$field_content = '';
		if($field_params['description']){
			$field_content .= '<div>'.$field_params['description'].'</div>';
		}		
			
		$field_content .= '<input type="text" class="width200" value="'.$field_value.'" ';
		if($field_params['max_chars']!=''){
			$field_content .= 'maxlength="'.$field_params['max_chars'].'" ';
		}
		$field_content .= 'name="'.$field_id.'[]" id="'.$field_id.'" />';
		$html .= $this->display_field($field_name, $field_content);
		$html .= '</div>';
		return $html;
	}
	
	function render_field_output($field, $intro_or_full, $readmore_type=0, $editor_id=0){
		return addslashes($field->value);
	}
}

//TEXTAREA
class class_fieldtype_textarea extends class_pi{
	
	function display_config_form($plugin, $type_id, $name, $field_params, $field_id){
		if(!$field_id){
			//new field, set defaults here
			$field_params['no_lines_to_breaks'] = 0;	
			$field_params['limit_characters'] = '0';		
		}
		//description	
		$html = $this->display_field_description($field_params);
		//validation
		$html .= $this->display_field_validation($field_params);
		//validation_mesage
		$html .= $this->display_field_validation_message($field_params);
		//no_lines_to_breaks
		$field_name = _pi_lang_no_lines_to_breaks;
		$field_content = '<input type="checkbox" ';
		if($this->check_if_field_param_is_present($field_params, 'no_lines_to_breaks')){
			if($field_params['no_lines_to_breaks']){
				$field_content .= ' checked="checked"';
			} 
		}
		$field_content .= 'name="field_params[no_lines_to_breaks]" value="1" />';	
		$html .= $this->display_field($field_name, $field_content);	
		//limit_characters
		$field_name = _pi_lang_limit_characters;
		$field_content = '<input type="text" value="';
		if($this->check_if_field_param_is_present($field_params, 'limit_characters')){
			$field_content .= $field_params['limit_characters'];
			if($field_params['limit_characters']==''){
				$field_content .= '0';
			}
		}
		$field_content .= '" name="field_params[limit_characters]" /> ';	
		$field_content .= _pi_lang_limit_characters2;
		$html .= $this->display_field($field_name, $field_content);
		//default value
		//$html .= $this->display_field_default_value($field_params);	
		$field_name = _pi_lang_default_value;
		$value = $field_params['default_value'];
		//$value = str_replace('<br />', "\n", $value);
		$field_content = '<textarea cols="100" rows="25"  name="default_value" >'.$value.'</textarea>';	
		$html .= $this->display_field($field_name, $field_content);		
		
		//no_lines_to_breaks	
		return $html;
	}
	
	
	function display_item_edit($field, $field_params, $field_values, $field_value, $new_field, $field_id){	
		//if new field, set defaults
		if($new_field){			
			$field_value = $field_params['default_value'];
		}
		
		//make html-breaks into line-breaks
		//$field_value = str_replace('<br />', "\n", $field_value);
		
		$html = '<div class="field_type_text fieldtype">';
		$html .= '<div class="pi_form_wrapper">';
		$html .= '<div class="pi_width20">';
		$html .= $field->name;
		if($this->check_if_field_param_is_present($field_params, 'description')){
			$html .= '<img src="'.$this->live_site.'/administrator/components/com_pi_pages_and_items/images/star.gif" alt="required field" />';
		}
		$html .= '</div>';
		$html .= '<div class="pi_width70">';
		if($field_params['description']){
			$html .= '<div>'.$field_params['description'].'</div>';
		}
		
		$limit_textfield_tags = '';
		if($field_params['limit_characters']){		
			$html .= '<script language="javascript" type="text/javascript">'."\n";
			//function to check chars left
			$html .= 'function limitText_'.$field_id.'(limitField, limitCount, limitNum) {'."\n";
			$html .= 'if (limitField.value.length > limitNum) {'."\n";
			$html .= 'limitField.value = limitField.value.substring(0, limitNum);'."\n";
			$html .= '} else {'."\n";
			$html .= ' limitCount.value = limitNum - limitField.value.length;'."\n";
			$html .= '}'."\n";
			$html .= '}'."\n";
			$html .= '</script>'."\n";
			$limit_textfield_tags = 'onKeyDown="limitText_'.$field_id.'(this.form.'.$field_id.',this.form.countdown_'.$field_id.','.$field_params['limit_characters'].');"
onKeyUp="limitText_'.$field_id.'(this.form.'.$field_id.',this.form.countdown_'.$field_id.','.$field_params['limit_characters'].');"';
		}
		$html .= '<textarea class="textarea" cols="60" rows="10" name="'.$field_id.'" id="'.$field_id.'" '.$limit_textfield_tags.'>'.$field_value.'</textarea>';
		if($field_params['limit_characters']){	
			$html .= '<br><input readonly type="text" name="countdown_'.$field_id.'" size="3" value="'.$field_params['limit_characters'].'"> '._pi_lang_characters_left;
		}
		$html .= '</div>';
		$html .= '</div>';
		$html .= '</div>';		
		
		return $html;
	}
	
	function field_save($field, $insert_or_update){		
			
		$value_name = 'field_values_'.$field->id;			
			
		//get vars
		if( defined('_JEXEC') ){
			//joomla 1.5
			$value = JRequest::getVar($value_name,'','post','string', JREQUEST_ALLOWRAW );
			$value = addslashes($value);	
		}else{
			//joomla 1.0.x
			$value = mosGetParam($_POST, $value_name,'', _MOS_ALLOWRAW);
		}			
		
		//make linebreaks into html-breaks
		//$value = str_replace("\n",'<br />', $value);
		//take out all other html tags
		//$value = strip_tags($value, '<br>');	
				
		return $value;
	}	
	
	
	function params_save($params_string){		
		if(defined('_JEXEC')){
			//joomla 1.5
			$default_value = JRequest::getVar('default_value','','post','string', JREQUEST_ALLOWRAW);							
		}else{
			//joomla 1.0.x					
			$default_value = mosGetParam( $_POST, 'default_value','', _MOS_ALLOWRAW);
			$default_value = stripslashes($default_value);
		}		
		
		//$default_value = str_replace("\n",'<br />', $default_value);
		
		$default_value = 'default_value-=-'.$default_value.'[;-)# ]';
		$default_temp = 'default_value-=-'.$this->get_field_param($params_string, 'default_value').'[;-)# ]';
		if(strpos($params_string, 'default_value-=-')){
			$params_string = str_replace($default_temp, $default_value, $params_string);			
		}else{
			$params_string .= $default_value;			
		}	
								
		//return $params_string;
		return $params_string;
	}
	
	
		
	function render_field_output($field, $intro_or_full, $readmore_type=0, $editor_id=0){
		$value = $field->value;
		if(!$this->get_field_param($field->params, 'no_lines_to_breaks')){
			$value = str_replace("\n",'<br />',$value);	
		}
		return addslashes($value);
	}
	
}

//PHP
class class_fieldtype_php extends class_pi{
	
	function params_base()
	{
		$param[] = 'no_pi_fish_table=1';
		return $param;
	}

	function display_config_form($plugin, $type_id, $name, $field_params, $field_id){
	
		if(!$field_id){
			$field_params['render'] = 'on_save';
			$field_params['display_when_edit'] = 'no';
			$field_params['validation'] = false;
		}	
		
		//render options		
		$field_name = _pi_lang_render_options;		
		//$field_content = 'code is rendered to content on save (best performance)<br />code is called when the item is viewed<br />';
		$field_content = '<label><input type="radio" class="radio" value="on_save" name="field_params[render]"';
		if($field_params['render']=='on_save' || $field_params['render']==''){
			$field_content .= 'checked="checked"';
		} 
		$field_content .= ' />';
		$field_content .= _pi_lang_render_when_save;
		$field_content .= '</label><br />';
		$field_content .= '<label><input type="radio" class="radio" value="on_the_fly" name="field_params[render]" ';
		if($field_params['render']=='on_the_fly'){
			$field_content .= 'checked="checked"';
		} 
		$field_content .= ' />';
		$field_content .= _pi_lang_render_on_the_fly;
		$field_content .= '</label>';	
		$html = $this->display_field($field_name, $field_content);	
	
	
		//display textarea for php code when item edit options	
		$field_name = _pi_lang_textarea_for_php_code;			
		
		$field_content = '<label><input type="radio" class="radio" value="yes" name="field_params[display_when_edit]"';
		if($field_params['display_when_edit']=='yes'){
			$field_content .= 'checked="checked"';
		} 
		$field_content .= ' onchange="document.getElementById(\'do_validation\').disabled=false;" />';
		$field_content .= _pi_lang_display_php_when_edit;
		$field_content .= '</label><br />';
		$field_content .= '<label><input type="radio" class="radio" value="no" name="field_params[display_when_edit]" ';
		if($field_params['display_when_edit']=='no' || $field_params['display_when_edit']==''){
			$field_content .= 'checked="checked"';
		} 
		$field_content .= ' onchange="document.getElementById(\'do_validation\').checked=false;document.getElementById(\'do_validation\').disabled=true;" />';
		$field_content .= _pi_lang_display_no_php_when_edit;
		$field_content .= '</label>';	
		$html .= $this->display_field($field_name, $field_content);	
	
	
		//description	
		$html .= $this->display_field_description($field_params);
		//validation		
		$field_name = _pi_lang_validation;
		$field_content = '<input type="checkbox" class="checkbox" ';		
		if($this->check_if_field_param_is_present($field_params, 'validation')){
			if($field_params['validation']){
				$field_content .= ' checked="checked"';
			}
		} 
		$field_content .= 'name="field_params[validation]" value="not_empty" id="do_validation" /> ';	
		$html .= $this->display_field($field_name, $field_content);
		//validation_mesage
		$html .= $this->display_field_validation_message($field_params);
		//default value
		$field_name = _pi_lang_default_value;		
		$field_content = $this->show_example_vars().'<textarea cols="100" rows="25"  name="default_value" >'.$field_params['default_value'].'</textarea>';			
		$html .= $this->display_field($field_name, $field_content);				
		return $html;
	}
	
	function params_save($params_string){		
		if(defined('_JEXEC')){
			//joomla 1.5
			$default_value = JRequest::getVar('default_value','','post','string', JREQUEST_ALLOWRAW);							
		}else{
			//joomla 1.0.x					
			$default_value = mosGetParam( $_POST, 'default_value','', _MOS_ALLOWRAW);
			$default_value = stripslashes($default_value);
		}		
		$default_value = str_replace('<br>','<br />', $default_value);
		$default_value = 'default_value-=-'.$default_value.'[;-)# ]';
		$default_temp = 'default_value-=-'.$this->get_field_param($params_string, 'default_value').'[;-)# ]';
		if(strpos($params_string, 'default_value-=-')){
			$params_string = str_replace($default_temp, $default_value, $params_string);
			//$params_string = 'string zat erin';
		}else{
			$params_string .= $default_value;
			//$params_string = 'string zat er niet in';
		}	
								
		//return $params_string;
		return $params_string;
	}	
	
	function display_item_edit($field, $field_params, $field_values, $field_value, $new_field, $field_id){	
	
		if($field_params['display_when_edit']=='yes'){
			//if new field, set defaults
			if($new_field){			
				$field_value = $field_params['default_value'];
			}
			
			$html = '<div class="field_type_html fieldtype">';
			$html .= '<div class="pi_form_wrapper">';
			$html .= '<div class="pi_width20">';
			$html .= $field->name;
			if($this->check_if_field_param_is_present($field_params, 'validation')){
				$html .= '<img src="'.$this->live_site.'/administrator/components/com_pi_pages_and_items/images/star.gif" alt="required field" />';
			}
			$html .= '</div>';
			$html .= '<div class="pi_width70">';
			if($field_params['description']){
				$html .= '<div>'.$field_params['description'].'</div>';
			}
			$html .= $this->show_example_vars();
			$html .= '<textarea class="textarea" cols="60" rows="10" name="'.$field_id.'" id="'.$field_id.'" >'.$field_value.'</textarea>';			
			$html .= '</div>';
			$html .= '</div>';
			$html .= '</div>';		
		}else{
			$html = '';
		}
		
		return $html;
	}
	
	function show_example_vars(){
		$html = _pi_lang_php_vars_available.': $item_id $field_id $value_id $database. '._pi_lang_example.': &lt;?php echo \'foo\'; ?&gt;';
		//$html .= '<br />';
		//$html .= 'functions:';
		//$html .= '<br />';
		//$html .= '$this->return_field_value($item_id, $field_id);';
		//$html .= '<br />';
		//$html .= '$this->get_field_value($values_string, $property);';
		return $html;
	}
	
	function render_field_output($field, $intro_or_full, $readmore_type=0, $editor_id=0){
		//when render output gets triggered is a good point to copy the params default value to the field-value. this exeptional method only happens in this php fieldtype.		
		
		if($this->get_field_param($field->params, 'display_when_edit')=='no'){	
			//update value field
			$value = $this->get_field_param($field->params, 'default_value');
			$value = addslashes($value);
			$value_id = $field->value_id;
			$database = $this->get_database();
			$database->setQuery( "UPDATE #__pi_custom_fields_values SET value='$value' WHERE id='$value_id' ");
			$database->query();
		}		
				
		//render html according to render options in field params.
		if($this->get_field_param($field->params, 'render')=='on_save'){
			//pre-render the html	
			$value_id = $field->value_id;
			$php_to_render = '<?php $value_id = \''.$value_id.'\'; 
			$item_id = \''.$field->item_id.'\'; 
			$field_id = \''.$field->field_id.'\'; ?>';			
			if($this->get_field_param($field->params, 'display_when_edit')=='no'){	
				//no php-editting on item-level, so grab the default value of field params			
				$php_to_render .= $this->get_field_param($field->params, 'default_value');
			}else{
				//php-editting on item level, so get the value from there. 
				$php_to_render .= $field->value;			
			}	
			
			
			$html = $this->phpWrapper($php_to_render);
			return $html;
		}else{
			//add a plugin-tag so the content will be rendered when it hits the onpreparecontent event of the pages-and-items content-plugin.
			return '{pi_dynamic_field php '.$field->value_id.'}';
		}			
	}
	
	function field_save($field, $insert_or_update){
		
		if($this->get_field_param($field->params, 'display_when_edit')=='yes'){
			
			$value_name = 'field_values_'.$field->id;			
			
			//get vars
			if( defined('_JEXEC') ){
				//joomla 1.5
				$value = JRequest::getVar($value_name,'','post','string', JREQUEST_ALLOWRAW );
				$value = addslashes($value);	
			}else{
				//joomla 1.0.x
				$value = mosGetParam( $_POST, $value_name,'', _MOS_ALLOWRAW);
			}
		}else{
			$value = addslashes($this->get_field_param($field->params, 'default_value'));
		}		
		$value = str_replace('<br>','<br />', $value);				
				
		return $value;
		
	}
	
	function get_database(){
		if(defined('_JEXEC')){
			//joomla 1.5
			$database = JFactory::getDBO();
		}else{
			//joomla 1.0.x
			global $database;
		}
		return $database;
	}
	
	function phpWrapper($content){
		$database = $this->get_database();		
		//$dingboops = new class_pi();							
		ob_start();
		eval("?>" . $content);
		$content = ob_get_contents();
		ob_end_clean(); 		
		return $content;
	}	
	
	
}

//CALENDAR
class class_fieldtype_calendar extends class_pi{	

	function params_base()
	{
		$param[] = 'only_once=1';
		$param[] = 'no_pi_fish_table=0';
		return $param;
	}
	
	function display_config_form($plugin, $type_id, $name, $field_params, $field_id){
		if(!$field_id){
			//new field, set defaults here
			$field_params['date_format'] = '%d-%m-%Y';
		}

		//display
		$field_name = _pi_lang_date_format;
		$field_content = '<input type="text" class="width200" value="';
		$field_content .= $field_params['date_format'];
		$field_content .= '" name="field_params[date_format]" />';
		$field_content .= '<br />';
		$field_content .= _pi_lang_example; 
		$field_content .= '<br />%d-%m-%Y<br /><a href="http://linux.about.com/od/commands/l/blcmdl1_date.htm" target="_blank">';
		$field_content .= 'http://linux.about.com/od/commands/l/blcmdl1_date.htm</a><br />';
		
		$html = $this->display_field($field_name, $field_content);
		
		return $html;
	}
	
	function display_item_edit($field, $field_params, $field_values, $field_value, $new_field, $field_id){			
		$html = '<div class="field_type_text fieldtype">';
		$field_name = $field->name;	
		$date = $this->get_date_to_format($field_value);			
		$field_content = JHTML::calendar($date,'calendar'.$field_id, 'calendar'.$field_id, $field_params['date_format']);		
		$html .= $this->display_field($field_name, $field_content);
		$html .= '</div>';
		return $html;	
		
	}
	
	function field_save($field, $insert_or_update){		
			
		$value_name = 'calendarfield_values_'.$field->id;		
		
		$value = JRequest::getVar($value_name,'','post');		
		if($value==_pi_lang_now || $value==''){			
			$value = $this->get_date_now(true);				
		}else{	
			if(defined('_JEXEC') ){
				//joomla 1.5
				//add empty time if only date was entered
				if ($value && strlen(trim($value)) <= 10){
					$value .= ' 00:00:00';
				}
			}			
		}		
		$value = $this->get_date_ready_for_database($value);
			
		return $value;
	}	
	
	function render_field_output($field, $intro_or_full, $readmore_type=0, $editor_id=0){		
		
		$date = strtotime($field->value);
		//$formatted_date = date($this->get_field_param($field->params, 'date_format'), $date);
		//get_date_to_format($date)
		
		$date = JFactory::getDate($date);
		$formatted_date = $date->toFormat($this->get_field_param($field->params, 'date_format'));
		return $formatted_date;
	}
}

//image_multisize
class class_fieldtype_image_multisize extends class_pi{
	
	function display_config_form($plugin, $type_id, $name, $field_params, $field_id){
		if(!$field_id){
			//new field, set defaults here
			$field_params['show_field_name'] = 0;	
			$field_params['delete_old_image'] = 0;			
			$field_params['image_dir'] = 'images/stories/';	
			$field_params['image_alt_auto'] = 1;			
			
			$field_params['max_width_1'] = '400';
			$field_params['max_height_1'] = '';			
			$field_params['show_src_1'] = 0;
			$field_params['class_name_1'] = '';				
			$field_params['only_source_1'] = 0;			
			$field_params['resize_1'] = 'resize';
			
			$field_params['enable_2'] = 0;
			$field_params['max_width_2'] = '';
			$field_params['max_height_2'] = '';			
			$field_params['show_src_2'] = 0;
			$field_params['class_name_2'] = '';				
			$field_params['only_source_2'] = 0;			
			$field_params['resize_2'] = 'resize';
			
			$field_params['enable_3'] = 0;
			$field_params['max_width_3'] = '';
			$field_params['max_height_3'] = '';			
			$field_params['show_src_3'] = 0;
			$field_params['class_name_3'] = '';				
			$field_params['only_source_3'] = 0;			
			$field_params['resize_3'] = 'resize';
			
			$field_params['enable_4'] = 0;
			$field_params['max_width_4'] = '';
			$field_params['max_height_4'] = '';			
			$field_params['show_src_4'] = 0;
			$field_params['class_name_4'] = '';				
			$field_params['only_source_4'] = 0;			
			$field_params['resize_4'] = 'resize';
			
			$field_params['enable_5'] = 0;
			$field_params['max_width_5'] = '';
			$field_params['max_height_5'] = '';			
			$field_params['show_src_5'] = 0;
			$field_params['class_name_5'] = '';				
			$field_params['only_source_5'] = 0;			
			$field_params['resize_5'] = 'resize';			
		}
		
		//warning for Joomla 1.0
		if(!defined('_JEXEC')){
			echo '<div class="pi_form_wrapper big_red_letter">This fieldtype does not work on Joomla 1.0.</div>';
		}
		
		//description	
		$html = $this->display_field_description($field_params);
		//show field name
		$field_name = _pi_lang_show_field_name;
		$field_content = '<input type="checkbox" ';			
		if(isset($field_params['show_field_name'])){
			$field_content .= ' checked="checked"';
		} 
		$field_content .= 'name="field_params[show_field_name]" value="1" />';	
		$html .= $this->display_field($field_name, $field_content);	
		//validation
		$field_name = _pi_lang_validation;
		$field_content = '<input type="checkbox" class="checkbox" ';				
		//if($this->check_if_field_param_is_present($field_params, 'validation')){
		if(isset($field_params['validation'])){
			if($field_params['validation']){
				$field_content .= ' checked="checked"';
			}
		} 
		$field_content .= 'name="field_params[validation]" value="not_empty" /> '._pi_lang_need_image;	
		$html .= $this->display_field($field_name, $field_content);	
		//validation_mesage
		$html .= $this->display_field_validation_message($field_params);	
		//image dir
		$field_name = _pi_lang_image_dir;
		$field_content = '<input type="text" class="width200" value="'.$field_params['image_dir'].'" name="field_params[image_dir]" /> '._pi_lang_image_dir_example_b;	
		$html .= $this->display_field($field_name, $field_content);	
		//old image
		$field_name = _pi_lang_delete_old_image;
		$field_content = '<input type="checkbox" class="checkbox" ';			
		if(isset($field_params['delete_old_image'])){
			$field_content .= ' checked="checked"';
		} 
		$field_content .= 'name="field_params[delete_old_image]" value="1" />';	
		$html .= $this->display_field($field_name, $field_content);			
		//image_alt_auto
		$field_name = _pi_lang_image_alt_auto;
		$field_content = '<input type="checkbox" class="checkbox" ';			
		if(isset($field_params['image_alt_auto'])){
			$field_content .= ' checked="checked"';
		} 
		$field_content .= 'name="field_params[image_alt_auto]" value="1" />'._pi_lang_image_alt_auto_info;	
		$html .= $this->display_field($field_name, $field_content);
		//info
		$html .= '<div class="pi_wrapper">'._pi_lang_image_resize_info.'.</div>';
		
		//image 1	
		$html .= '<br /><br /><br /><h3>'._pi_lang_image.' '._pi_lang_size.' 1</h3>';
		
			
		//width
		$field_name = _pi_lang_max_width;
		$field_content = '<input type="text" class="width200" value="'.$field_params['max_width_1'].'" name="field_params[max_width_1]" />';	
		$html .= $this->display_field($field_name, $field_content);	
		//height
		$field_name = _pi_lang_max_height;
		$field_content = '<input type="text" class="width200" value="'.$field_params['max_height_1'].'" name="field_params[max_height_1]" />';	
		$html .= $this->display_field($field_name, $field_content);	
		//resize
		$field_name = _pi_lang_resize_options;		
		$field_content = '<label><input type="radio" class="radio" value="none" name="field_params[resize_1]"';
		if($field_params['resize_1']=='none'){
			$field_content .= 'checked="checked"';
		} 
		$field_content .= ' />';
		$field_content .= _pi_lang_no_resize;
		$field_content .= '</label><br />';	
		$field_content .= '<label><input type="radio" class="radio" value="resize" name="field_params[resize_1]"';
		if($field_params['resize_1']=='resize'){
			$field_content .= 'checked="checked"';
		} 
		$field_content .= ' />';
		$field_content .= _pi_lang_do_resize.'.';
		$field_content .= '</label><br />';	
		$field_content .= '<label><input type="radio" class="radio" value="force" name="field_params[resize_1]"';
		if($field_params['resize_1']=='force'){
			$field_content .= 'checked="checked"';
		} 
		$field_content .= ' />';
		$field_content .= _pi_lang_force_resize.'.';
		$field_content .= '</label><br />';	
		$html .= $this->display_field($field_name, $field_content);			
		//classname
		$field_name = _pi_lang_class_name;
		$field_content = '<input type="text" class="width200" value="'.$field_params['class_name_1'].'" name="field_params[class_name_1]" /> '._pi_lang_class_name2;	
		$html .= $this->display_field($field_name, $field_content);	
		//output_only_source
		$field_name = _pi_lang_output_only_source;
		$field_content = '<input type="checkbox" class="checkbox" ';		
		if(isset($field_params['only_source_1'])){
			if($field_params['only_source_1']){
				$field_content .= ' checked="checked"';
			}
		} 
		$field_content .= 'name="field_params[only_source_1]" value="1" />';	
		$html .= $this->display_field($field_name, $field_content);	
		//template code
		$field_name = _pi_lang_template_code;
		if(!$field_id){
			$field_content = '{field_multisize_121 size=1}<br />{field_multisize_121 output=alt}<br />'._pi_lang_example_multisize;	
		}else{
			$field_content = '{field_multisize_'.$field_id.' size=1}<br />{field_multisize_'.$field_id.' output=alt}';	
		}
		
		$html .= $this->display_field($field_name, $field_content);	
		
		
		//image 2	
		$html .= '<br /><br /><br /><h3>'._pi_lang_image.' '._pi_lang_size.' 2</h3>';
		
		//enable
		$field_name = _pi_lang_enable;
		$field_content = '<input type="checkbox" class="checkbox" ';		
		if(isset($field_params['enable_2'])){
			if($field_params['enable_2']){
				$field_content .= ' checked="checked"';
			}
		} 
		$field_content .= 'name="field_params[enable_2]" value="1" />';
		$html .= $this->display_field($field_name, $field_content);	
		//width
		$field_name = _pi_lang_max_width;
		$field_content = '<input type="text" class="width200" value="'.$field_params['max_width_2'].'" name="field_params[max_width_2]" />';	
		$html .= $this->display_field($field_name, $field_content);	
		//height
		$field_name = _pi_lang_max_height;
		$field_content = '<input type="text" class="width200" value="'.$field_params['max_height_2'].'" name="field_params[max_height_2]" />';	
		$html .= $this->display_field($field_name, $field_content);	
		//resize
		$field_name = _pi_lang_resize_options;		
		$field_content = '<label><input type="radio" class="radio" value="none" name="field_params[resize_2]"';
		if($field_params['resize_2']=='none'){
			$field_content .= 'checked="checked"';
		} 
		$field_content .= ' />';
		$field_content .= _pi_lang_no_resize;
		$field_content .= '</label><br />';	
		$field_content .= '<label><input type="radio" class="radio" value="resize" name="field_params[resize_2]"';
		if($field_params['resize_2']=='resize'){
			$field_content .= 'checked="checked"';
		} 
		$field_content .= ' />';
		$field_content .= _pi_lang_do_resize.'.';
		$field_content .= '</label><br />';	
		$field_content .= '<label><input type="radio" class="radio" value="force" name="field_params[resize_2]"';
		if($field_params['resize_2']=='force'){
			$field_content .= 'checked="checked"';
		} 
		$field_content .= ' />';
		$field_content .= _pi_lang_force_resize.'.';
		$field_content .= '</label><br />';	
		$html .= $this->display_field($field_name, $field_content);	
		//classname
		$field_name = _pi_lang_class_name;
		$field_content = '<input type="text" class="width200" value="'.$field_params['class_name_2'].'" name="field_params[class_name_2]" /> '._pi_lang_class_name2;	
		$html .= $this->display_field($field_name, $field_content);	
		//output_only_source
		$field_name = _pi_lang_output_only_source;
		$field_content = '<input type="checkbox" class="checkbox" ';		
		if(isset($field_params['only_source_2'])){
			if($field_params['only_source_2']){
				$field_content .= ' checked="checked"';
			}
		} 
		$field_content .= 'name="field_params[only_source_2]" value="1" />';	
		$html .= $this->display_field($field_name, $field_content);	
		//template code
		$field_name = _pi_lang_template_code;
		if(!$field_id){
			$field_content = '{field_multisize_121 size=2}<br />{field_multisize_121 output=alt}<br />'._pi_lang_example_multisize;	
		}else{
			$field_content = '{field_multisize_'.$field_id.' size=2}<br />{field_multisize_'.$field_id.' output=alt}';	
		}
		
		$html .= $this->display_field($field_name, $field_content);	
		
		//image 3	
		$html .= '<br /><br /><br /><h3>'._pi_lang_image.' '._pi_lang_size.' 3</h3>';
		
		//enable
		$field_name = _pi_lang_enable;
		$field_content = '<input type="checkbox" class="checkbox" ';		
		if(isset($field_params['enable_3'])){
			if($field_params['enable_3']){
				$field_content .= ' checked="checked"';
			}
		} 
		$field_content .= 'name="field_params[enable_3]" value="1" />';
		$html .= $this->display_field($field_name, $field_content);	
		//width
		$field_name = _pi_lang_max_width;
		$field_content = '<input type="text" class="width200" value="'.$field_params['max_width_3'].'" name="field_params[max_width_3]" />';	
		$html .= $this->display_field($field_name, $field_content);	
		//height
		$field_name = _pi_lang_max_height;
		$field_content = '<input type="text" class="width200" value="'.$field_params['max_height_3'].'" name="field_params[max_height_3]" />';	
		$html .= $this->display_field($field_name, $field_content);	
		//resize
		$field_name = _pi_lang_resize_options;		
		$field_content = '<label><input type="radio" class="radio" value="none" name="field_params[resize_3]"';
		if($field_params['resize_3']=='none'){
			$field_content .= 'checked="checked"';
		} 
		$field_content .= ' />';
		$field_content .= _pi_lang_no_resize;
		$field_content .= '</label><br />';	
		$field_content .= '<label><input type="radio" class="radio" value="resize" name="field_params[resize_3]"';
		if($field_params['resize_3']=='resize'){
			$field_content .= 'checked="checked"';
		} 
		$field_content .= ' />';
		$field_content .= _pi_lang_do_resize.'.';
		$field_content .= '</label><br />';	
		$field_content .= '<label><input type="radio" class="radio" value="force" name="field_params[resize_3]"';
		if($field_params['resize_3']=='force'){
			$field_content .= 'checked="checked"';
		} 
		$field_content .= ' />';
		$field_content .= _pi_lang_force_resize.'.';
		$field_content .= '</label><br />';	
		$html .= $this->display_field($field_name, $field_content);	
		//classname
		$field_name = _pi_lang_class_name;
		$field_content = '<input type="text" class="width200" value="'.$field_params['class_name_3'].'" name="field_params[class_name_3]" /> '._pi_lang_class_name2;	
		$html .= $this->display_field($field_name, $field_content);	
		//output_only_source
		$field_name = _pi_lang_output_only_source;
		$field_content = '<input type="checkbox" class="checkbox" ';		
		if(isset($field_params['only_source_3'])){
			if($field_params['only_source_3']){
				$field_content .= ' checked="checked"';
			}
		} 
		$field_content .= 'name="field_params[only_source_3]" value="1" />';	
		$html .= $this->display_field($field_name, $field_content);	
		//template code
		$field_name = _pi_lang_template_code;
		if(!$field_id){
			$field_content = '{field_multisize_121 size=3}<br />{field_multisize_121 output=alt}<br />'._pi_lang_example_multisize;	
		}else{
			$field_content = '{field_multisize_'.$field_id.' size=3}<br />{field_multisize_'.$field_id.' output=alt}';	
		}
		
		$html .= $this->display_field($field_name, $field_content);	
		
		//image 4	
		$html .= '<br /><br /><br /><h3>'._pi_lang_image.' '._pi_lang_size.' 4</h3>';
		
		//enable
		$field_name = _pi_lang_enable;
		$field_content = '<input type="checkbox" class="checkbox" ';		
		if(isset($field_params['enable_4'])){
			if($field_params['enable_4']){
				$field_content .= ' checked="checked"';
			}
		} 
		$field_content .= 'name="field_params[enable_4]" value="1" />';
		$html .= $this->display_field($field_name, $field_content);	
		//width
		$field_name = _pi_lang_max_width;
		$field_content = '<input type="text" class="width200" value="'.$field_params['max_width_4'].'" name="field_params[max_width_4]" />';	
		$html .= $this->display_field($field_name, $field_content);	
		//height
		$field_name = _pi_lang_max_height;
		$field_content = '<input type="text" class="width200" value="'.$field_params['max_height_4'].'" name="field_params[max_height_4]" />';	
		$html .= $this->display_field($field_name, $field_content);	
		//resize
		$field_name = _pi_lang_resize_options;		
		$field_content = '<label><input type="radio" class="radio" value="none" name="field_params[resize_4]"';
		if($field_params['resize_4']=='none'){
			$field_content .= 'checked="checked"';
		} 
		$field_content .= ' />';
		$field_content .= _pi_lang_no_resize;
		$field_content .= '</label><br />';	
		$field_content .= '<label><input type="radio" class="radio" value="resize" name="field_params[resize_4]"';
		if($field_params['resize_4']=='resize'){
			$field_content .= 'checked="checked"';
		} 
		$field_content .= ' />';
		$field_content .= _pi_lang_do_resize.'.';
		$field_content .= '</label><br />';	
		$field_content .= '<label><input type="radio" class="radio" value="force" name="field_params[resize_4]"';
		if($field_params['resize_4']=='force'){
			$field_content .= 'checked="checked"';
		} 
		$field_content .= ' />';
		$field_content .= _pi_lang_force_resize.'.';
		$field_content .= '</label><br />';	
		$html .= $this->display_field($field_name, $field_content);	
		//classname
		$field_name = _pi_lang_class_name;
		$field_content = '<input type="text" class="width200" value="'.$field_params['class_name_4'].'" name="field_params[class_name_4]" /> '._pi_lang_class_name2;	
		$html .= $this->display_field($field_name, $field_content);	
		//output_only_source
		$field_name = _pi_lang_output_only_source;
		$field_content = '<input type="checkbox" class="checkbox" ';		
		if(isset($field_params['only_source_4'])){
			if($field_params['only_source_4']){
				$field_content .= ' checked="checked"';
			}
		} 
		$field_content .= 'name="field_params[only_source_4]" value="1" />';	
		$html .= $this->display_field($field_name, $field_content);	
		//template code
		$field_name = _pi_lang_template_code;
		if(!$field_id){
			$field_content = '{field_multisize_121 size=4}<br />{field_multisize_121 output=alt}<br />'._pi_lang_example_multisize;	
		}else{
			$field_content = '{field_multisize_'.$field_id.' size=4}<br />{field_multisize_'.$field_id.' output=alt}';	
		}
		
		$html .= $this->display_field($field_name, $field_content);	
		//image 5	
		$html .= '<br /><br /><br /><h3>'._pi_lang_image.' '._pi_lang_size.' 5</h3>';
		
		//enable
		$field_name = _pi_lang_enable;
		$field_content = '<input type="checkbox" class="checkbox" ';		
		if(isset($field_params['enable_5'])){
			if($field_params['enable_5']){
				$field_content .= ' checked="checked"';
			}
		} 
		$field_content .= 'name="field_params[enable_5]" value="1" />';
		$html .= $this->display_field($field_name, $field_content);	
		//width
		$field_name = _pi_lang_max_width;
		$field_content = '<input type="text" class="width200" value="'.$field_params['max_width_5'].'" name="field_params[max_width_5]" />';	
		$html .= $this->display_field($field_name, $field_content);	
		//height
		$field_name = _pi_lang_max_height;
		$field_content = '<input type="text" class="width200" value="'.$field_params['max_height_5'].'" name="field_params[max_height_5]" />';	
		$html .= $this->display_field($field_name, $field_content);	
		//resize
		$field_name = _pi_lang_resize_options;		
		$field_content = '<label><input type="radio" class="radio" value="none" name="field_params[resize_5]"';
		if($field_params['resize_5']=='none'){
			$field_content .= 'checked="checked"';
		} 
		$field_content .= ' />';
		$field_content .= _pi_lang_no_resize;
		$field_content .= '</label><br />';	
		$field_content .= '<label><input type="radio" class="radio" value="resize" name="field_params[resize_5]"';
		if($field_params['resize_5']=='resize'){
			$field_content .= 'checked="checked"';
		} 
		$field_content .= ' />';
		$field_content .= _pi_lang_do_resize.'.';
		$field_content .= '</label><br />';	
		$field_content .= '<label><input type="radio" class="radio" value="force" name="field_params[resize_5]"';
		if($field_params['resize_5']=='force'){
			$field_content .= 'checked="checked"';
		} 
		$field_content .= ' />';
		$field_content .= _pi_lang_force_resize.'.';
		$field_content .= '</label><br />';	
		$html .= $this->display_field($field_name, $field_content);	
		//classname
		$field_name = _pi_lang_class_name;
		$field_content = '<input type="text" class="width200" value="'.$field_params['class_name_5'].'" name="field_params[class_name_5]" /> '._pi_lang_class_name2;	
		$html .= $this->display_field($field_name, $field_content);	
		//output_only_source
		$field_name = _pi_lang_output_only_source;
		$field_content = '<input type="checkbox" class="checkbox" ';		
		if(isset($field_params['only_source_5'])){
			if($field_params['only_source_5']){
				$field_content .= ' checked="checked"';
			}
		} 
		$field_content .= 'name="field_params[only_source_5]" value="1" />';	
		$html .= $this->display_field($field_name, $field_content);
		//template code
		$field_name = _pi_lang_template_code;
		if(!$field_id){
			$field_content = '{field_multisize_121 size=5}<br />{field_multisize_121 output=alt}<br />'._pi_lang_example_multisize;	
		}else{
			$field_content = '{field_multisize_'.$field_id.' size=5}<br />{field_multisize_'.$field_id.' output=alt}';	
		}
		
		$html .= $this->display_field($field_name, $field_content);		
		
		return $html;
	}
	
	function display_item_edit($field, $field_params, $field_values, $field_value, $new_field, $field_id){	
		if($new_field){
			//new field		
			$src = '';
			$alt = '';
			$field_values['image_alt_auto'] = 1;
		}else{
			//edit field
			if(isset($field_values['src'])){
				$src = $field_values['src'];
			}else{
				$src = 0;
			}			
			if(isset($field_values['alt'])){			
				$alt = $field_values['alt'];
			}else{				
				$alt = '';
			}			
		}
				
		$html = '<div class="field_type_image fieldtype">';	
		
		if(isset($field_params['show_field_name'])){		
			$html .= '<div>'.$field->name.'</div>';
		}
		if($field_params['description']){
			$html .= '<div>'.$field_params['description'].'</div>';
		} 
		
		$html .= '<div class="pi_form_wrapper">';
		$html .= '<div class="pi_width20">';
		$html .= '<span onMouseOver="return overlib(\''._pi_lang_image_tip.'\', CAPTION, \''._pi_lang_image.'\', BELOW, RIGHT);" onMouseOut="return nd();"  class="editlinktip">';
		$html .= _pi_lang_image;
		$html .= '</span>';				
		if(isset($field_params['validation'])){	
			$html .= '<img src="'.$this->live_site.'/administrator/components/com_pi_pages_and_items/images/star.gif" alt="required field" />';
		}
		$html .= '</div>';
		$html .= '<div class="pi_width70">';
		$html .= '<script language="javascript"  type="text/javascript">'."\n";
		$html .= 'function check_extension_'.$field_id.'(value){'."\n";	
		$html .= 'value = value.toLowerCase();'."\n";	
		$html .= 'pos_jpg = value.indexOf(".jpg");'."\n";
		$html .= 'pos_jpeg = value.indexOf(".jpeg");'."\n";
		$html .= 'pos_gif = value.indexOf(".gif");'."\n";	
		$html .= 'pos_png = value.indexOf(".png");'."\n";		
		$html .= 'if(pos_jpg==-1 && pos_jpeg==-1 && pos_gif==-1 && pos_png==-1){'."\n";
		$html .= 'document.getElementById(\''.$field_id.'_image\').value = \'\';'."\n";		
		$html .= 'alert(\'wrong file-type. allowed are: gif, png, jpg and jpeg\')'."\n";		
		$html .= '}'."\n";
		$html .= '}'."\n";
		$html .= '</script>'."\n";
		$html .= '<input type="file" value="1" name="'.$field_id.'_image" id="'.$field_id.'_image" onchange="check_extension_'.$field_id.'(this.value);" />';
		$html .= '</div>';
		$html .= '</div>';
		if($src){
			$html .= '<div class="pi_form_wrapper">';
			$html .= '<div class="pi_width20">';
			$html .= '&nbsp;';
			$html .= '</div>';
			$html .= '<div class="pi_width70">';
			$html .= '<div style="width: 400px; overflow-x: auto; overflow-y: hidden;">';
			//$html .= '<img src="../images/stories/'.$src.'" alt="'.$src.'" />';	
			$image_dir = $this->alter_image_dir($field_params['image_dir'], $field->item_id);
			$html .= '<img src="'.$this->live_site.$image_dir.$src.'" alt="'.$src.'" />';			
			$html .= '</div>';
			$html .= '</div>';
			$html .= '</div>';
			$html .= '<div class="pi_form_wrapper">';
			$html .= '<div class="pi_width20">';
			$html .= _pi_lang_delete_image;
			$html .= ':</div>';
			$html .= '<div class="pi_width70">';
			$html .= '<input type="checkbox" class="checkbox" name="'.$field_id.'_delete_image" value="1" />';
			$html .= '</div>';
			$html .= '</div>';	
			if(isset($field_params['show_src'])){
				$html .= '<div class="pi_form_wrapper">';
				$html .= '<div class="pi_width20">';
				$html .= _pi_lang_src;
				$html .= ':</div>';
				$html .= '<div class="pi_width70">';
				$html .= '<input type="text" class="width200" value="';
				//$html .= $field_params['image_dir'];
				$html .= $field_values['src'].'" name="'.$field_id.'_src" id="'.$field_id.'_src" />';								
				$html .= '</div>';
				$html .= '</div>';
			}else{
				$html .= '<input type="hidden" class="width200" value="';
				//$html .= $field_params['image_dir'];
				$html .= $field_values['src'].'" name="'.$field_id.'_src" id="'.$field_id.'_src" />';	
			}
		}else{
			echo '<input type="hidden" class="width200" value=""  name="'.$field_id.'_src" id="'.$field_id.'_src" />';
		}
		$html .= '<div class="pi_form_wrapper">';
		$html .= '<div class="pi_width20">';
		$html .= _pi_lang_alt_text;
		$html .= ':</div>';
		$html .= '<div class="pi_width70">';
		$html .= '<input type="text" class="width200" value="'.$alt.'" name="'.$field_id.'_alt" />';				
		$html .= '</div>';
		$html .= '</div>';
		$html .= '</div>';	
		//parse general field settings	
		$html .= '<input type="hidden" name="'.$field_id.'_delete_old_image" value="';		
		if(isset($field_params['delete_old_image'])){
			$html .= '1';
		}
		$html .=  '" />';			
		$html .= '<input type="hidden" name="'.$field_id.'_image_dir" value="';			
		if(isset($field_params['image_dir'])){			
			$html .= $field_params['image_dir'];
		}
		$html .=  '" />';
		if(isset($field_params['image_alt_auto'])){	
			$html .= '<input type="hidden" name="'.$field_id.'_image_alt_auto" value="1" />';	
		}
		//parse field setting for each of the image-sizes
		//image size 1
		$html .= '<input type="hidden" name="'.$field_id.'_max_width_1" value="'.$field_params['max_width_1'].'" />';
		$html .= '<input type="hidden" name="'.$field_id.'_max_height_1" value="'.$field_params['max_height_1'].'" />';	
		$html .= '<input type="hidden" name="'.$field_id.'_resize_1" value="'.$field_params['resize_1'].'" />';	
		$html .= '<input type="hidden" name="'.$field_id.'_class_name_1" value="'.$field_params['class_name_1'].'" />';	
		if(isset($field_params['only_source_1'])){	
			$html .= '<input type="hidden" name="'.$field_id.'_only_source_1" value="'.$field_params['only_source_1'].'" />';	
		}
		
		//image size 2
		if(isset($field_params['enable_2'])){	
			$html .= '<input type="hidden" name="'.$field_id.'_enable_2" value="'.$field_params['enable_2'].'" />';
		}
		$html .= '<input type="hidden" name="'.$field_id.'_max_width_2" value="'.$field_params['max_width_2'].'" />';
		$html .= '<input type="hidden" name="'.$field_id.'_max_height_2" value="'.$field_params['max_height_2'].'" />';	
		$html .= '<input type="hidden" name="'.$field_id.'_resize_2" value="'.$field_params['resize_2'].'" />';	
		$html .= '<input type="hidden" name="'.$field_id.'_class_name_2" value="'.$field_params['class_name_2'].'" />';	
		if(isset($field_params['only_source_2'])){		
			$html .= '<input type="hidden" name="'.$field_id.'_only_source_2" value="'.$field_params['only_source_2'].'" />';
		}	
		
		//image size 3
		if(isset($field_params['enable_3'])){	
			$html .= '<input type="hidden" name="'.$field_id.'_enable_3" value="'.$field_params['enable_3'].'" />';
		}
		$html .= '<input type="hidden" name="'.$field_id.'_max_width_3" value="'.$field_params['max_width_3'].'" />';
		$html .= '<input type="hidden" name="'.$field_id.'_max_height_3" value="'.$field_params['max_height_3'].'" />';	
		$html .= '<input type="hidden" name="'.$field_id.'_resize_3" value="'.$field_params['resize_3'].'" />';	
		$html .= '<input type="hidden" name="'.$field_id.'_class_name_3" value="'.$field_params['class_name_3'].'" />';	
		if(isset($field_params['only_source_3'])){		
			$html .= '<input type="hidden" name="'.$field_id.'_only_source_3" value="'.$field_params['only_source_3'].'" />';	
		}
		
		//image size 4
		if(isset($field_params['enable_4'])){	
			$html .= '<input type="hidden" name="'.$field_id.'_enable_4" value="'.$field_params['enable_4'].'" />';
		}
		$html .= '<input type="hidden" name="'.$field_id.'_max_width_4" value="'.$field_params['max_width_4'].'" />';
		$html .= '<input type="hidden" name="'.$field_id.'_max_height_4" value="'.$field_params['max_height_4'].'" />';	
		$html .= '<input type="hidden" name="'.$field_id.'_resize_4" value="'.$field_params['resize_4'].'" />';	
		$html .= '<input type="hidden" name="'.$field_id.'_class_name_4" value="'.$field_params['class_name_4'].'" />';	
		if(isset($field_params['only_source_4'])){		
			$html .= '<input type="hidden" name="'.$field_id.'_only_source_4" value="'.$field_params['only_source_4'].'" />';	
		}
		
		//image size 5
		if(isset($field_params['enable_5'])){	
			$html .= '<input type="hidden" name="'.$field_id.'_enable_5" value="'.$field_params['enable_5'].'" />';
		}
		$html .= '<input type="hidden" name="'.$field_id.'_max_width_5" value="'.$field_params['max_width_5'].'" />';
		$html .= '<input type="hidden" name="'.$field_id.'_max_height_5" value="'.$field_params['max_height_5'].'" />';	
		$html .= '<input type="hidden" name="'.$field_id.'_resize_5" value="'.$field_params['resize_5'].'" />';	
		$html .= '<input type="hidden" name="'.$field_id.'_class_name_5" value="'.$field_params['class_name_5'].'" />';	
		if(isset($field_params['only_source_5'])){	
			$html .= '<input type="hidden" name="'.$field_id.'_only_source_5" value="'.$field_params['only_source_5'].'" />';	
		}
		return $html;
	}
	
	function alter_image_dir($image_dir, $item_id){
		$image_dir = str_replace('$item_id', $item_id, $image_dir);
		return $image_dir;
	}
	
	function render_field_output($field, $intro_or_full, $readmore_type=0, $editor_id=0){
		$html = '';
				
		if($this->get_field_value($field->value, 'src')!=''){	
				
			$output = '';
			if(isset($field->output)){
				if($field->output=='alt'){
					$output = 'alt';
				}
			}			
				
			
			$file_name_src = $this->get_field_value($field->value, 'src');
			$file_name_array = explode('.',$file_name_src);
			$file_name_stuff = $file_name_array[0];					
			$file_name_base_array = explode('_size',$file_name_stuff);			
			$file_name_base = $file_name_base_array[0];				
			$file_name_extension = $file_name_array[1];	
			
			$alt = $this->get_field_value($field->value, 'alt');
			if($this->get_field_param($field->params, 'image_alt_auto') && $alt==''){
				$alt = $file_name_base.'.'.$file_name_extension;
			}
						
			if($output=='alt'){							
				$html .= $alt;													
			}else{					
				$only_source = $this->get_field_param($field->params, 'only_source_'.$field->size);
				$image_dir = $this->get_field_param($field->params, 'image_dir');
				$image_dir = $this->alter_image_dir($image_dir, $field->item_id);
				if(!$only_source){	
					//output full image						
					$html .= '<img src="';				
					$html .= $image_dir;						
					$html .= $file_name_base.'_size'.$field->size.'.'.$file_name_extension;						
					$html .= '" alt="'.$alt.'"';
					if($this->get_field_param($field->params, 'class_name')!=''){
						$html .= ' class="'.$this->get_field_param($field->params, 'class_name').'"';
					}	
					$html .= ' />';			
				}else{
					//only output source					
					$html .= $image_dir.$file_name_base.'_size'.$field->size.'.'.$file_name_extension;	
				}
			}
		}		
		
		return $html;		
	}
	
	function field_save($field, $insert_or_update){	
		
		$value_name = 'field_values_'.$field->id;		
		
		$image = $value_name.'_image';
		$src = $this->get_var($value_name.'_src', false);
		$alt = $this->get_var($value_name.'_alt');
		$image_alt_auto = $this->get_var($value_name.'_image_alt_auto');
		$delete_old_image = $this->get_var($value_name.'_delete_old_image');
		$delete_image = $this->get_var($value_name.'_delete_image', false);
		$image_dir = $this->get_var($value_name.'_image_dir', false);
		$image_dir = $this->alter_image_dir($image_dir, $field->item_id);		
		
		$max_width_1 = $this->get_var($value_name.'_max_width_1');
		$max_height_1 = $this->get_var($value_name.'_max_height_1');
		$resize_1 = $this->get_var($value_name.'_resize_1');	
		$class_name_1 = $this->get_var($value_name.'_class_name_1');
		$only_source_1 = $this->get_var($value_name.'_only_source_1');	
		
		$enable_2 = $this->get_var($value_name.'_enable_2');	
		$max_width_2 = $this->get_var($value_name.'_max_width_2');
		$max_height_2 = $this->get_var($value_name.'_max_height_2');
		$resize_2 = $this->get_var($value_name.'_resize_2');	
		$class_name_2 = $this->get_var($value_name.'_class_name_2');
		$only_source_2 = $this->get_var($value_name.'_only_source_2');	
		
		$enable_3 = $this->get_var($value_name.'_enable_3');	
		$max_width_3 = $this->get_var($value_name.'_max_width_3');
		$max_height_3 = $this->get_var($value_name.'_max_height_3');
		$resize_3 = $this->get_var($value_name.'_resize_3');	
		$class_name_3 = $this->get_var($value_name.'_class_name_3');
		$only_source_3 = $this->get_var($value_name.'_only_source_3');	
		
		$enable_4 = $this->get_var($value_name.'_enable_4');	
		$max_width_4 = $this->get_var($value_name.'_max_width_4');
		$max_height_4 = $this->get_var($value_name.'_max_height_4');
		$resize_4 = $this->get_var($value_name.'_resize_4');	
		$class_name_4 = $this->get_var($value_name.'_class_name_4');
		$only_source_4 = $this->get_var($value_name.'_only_source_4');	
		
		$enable_5 = $this->get_var($value_name.'_enable_5');	
		$max_width_5 = $this->get_var($value_name.'_max_width_5');
		$max_height_5 = $this->get_var($value_name.'_max_height_5');
		$resize_5 = $this->get_var($value_name.'_resize_5');	
		$class_name_5 = $this->get_var($value_name.'_class_name_5');
		$only_source_5 = $this->get_var($value_name.'_only_source_5');				
		
		$image_upload = false;
		
		//check if there is an image to upload	
		$userfile_name = strtolower($_FILES[$image]['name']);	
		if($userfile_name && !$delete_image){
			$image_upload = true;
		}		
		
		//get file name and extension
		$old_name = '';			
		$extension = '';	
		if($userfile_name){
			$file_temp = explode('.',$userfile_name);
			$old_name = $file_temp[0];			
			$extension = $file_temp[1];
		}	
		
		//when edit
		$source_bits = explode('/',$src);
		$source_bits = array_reverse($source_bits);
		$src_delete = $source_bits[0];
		$src_file_temp = explode('.',$src_delete);
		$src_old_name = $src_file_temp[0];			
		$src_extension = $src_file_temp[1];							
		$src_old_name2 = explode('_size',$src_old_name);			
		$src_old_name_part = $src_old_name2[0];	
		
		//if image edit, delete old image			
		if(($userfile_name && $src) || $delete_image){	
					
			if(file_exists(dirname(__FILE__).'/../../../'.$image_dir.'/'.$src_old_name_part.'_size1.'.$src_extension)){
				unlink(dirname(__FILE__).'/../../../'.$image_dir.'/'.$src_old_name_part.'_size1.'.$src_extension);	
			}
			if(file_exists(dirname(__FILE__).'/../../../'.$image_dir.'/'.$src_old_name_part.'_size2.'.$src_extension)){
				unlink(dirname(__FILE__).'/../../../'.$image_dir.'/'.$src_old_name_part.'_size2.'.$src_extension);	
			}
			if(file_exists(dirname(__FILE__).'/../../../'.$image_dir.'/'.$src_old_name_part.'_size3.'.$src_extension)){
				unlink(dirname(__FILE__).'/../../../'.$image_dir.'/'.$src_old_name_part.'_size3.'.$src_extension);	
			}
			if(file_exists(dirname(__FILE__).'/../../../'.$image_dir.'/'.$src_old_name_part.'_size4.'.$src_extension)){
				unlink(dirname(__FILE__).'/../../../'.$image_dir.'/'.$src_old_name_part.'_size4.'.$src_extension);	
			}
			if(file_exists(dirname(__FILE__).'/../../../'.$image_dir.'/'.$src_old_name_part.'_size5.'.$src_extension)){
				unlink(dirname(__FILE__).'/../../../'.$image_dir.'/'.$src_old_name_part.'_size5.'.$src_extension);	
			}						
		}
		
		//upload image		
		if($image_upload==true){			
						
			$userfile_tmp = $_FILES[$image]['tmp_name'];
			$userfile_size = $_FILES[$image]['size'];
			$userfile_type = $_FILES[$image]['type'];
			
			if (isset($_FILES[$image]['name'])){	
			
				//upload so make the folder here if we need to
				if (!JFolder::exists(JPATH_SITE.DS.$image_dir)){
					JFolder::create(JPATH_SITE.DS.$image_dir);
				}			
				
				//check extension
				$allowed_ext = "jpg jpeg gif png";			
				$allowed_extensions = explode(" ", $allowed_ext);			
				if (!in_array($extension, $allowed_extensions)){
					die('wrong file-type. allowed are: '.$allowed_ext);
				}			
				
				//rewrite jpeg to jpg
				if($extension=='jpeg'){
					$extension = 'jpg';
				}								
				
				//rename file if already exist
				if(file_exists(dirname(__FILE__).'/../../../'.$image_dir.'/'.$old_name.'_size1.'.$extension)){			
					$j = 2;
					while (file_exists(dirname(__FILE__).'/../../../'.$image_dir.'/'.$old_name.'-'.$j."_size1.".$extension)){
						$j = $j + 1;
					}
					$new_name = $old_name . "-" . $j;	
				}else{
					$new_name = $old_name;
				}
				
				//replace spaces by underscores
				$new_name = str_replace(' ', '_', $new_name);			
				
				//upload original image
				$prod_img = dirname(__FILE__).'/../../../'.$image_dir.'/'.$new_name.'_ori.'.$extension;
				
				
				if(!move_uploaded_file($userfile_tmp, $prod_img)){
					//die('Problem uploading image. File is too big. Try making the image smaller with an image-editor (like Photoshop or Gimp) and try again.');
				}				
				
				//get sizes and ratio
				$sizes = getimagesize($prod_img);
				$old_width = $sizes[0];
				$old_height = $sizes[1];
				$aspect_ratio = $sizes[1]/$sizes[0]; 
				
				//size 1
				$prod_img_size_1 = dirname(__FILE__).'/../../../'.$image_dir.'/'.$new_name.'_size1.'.$extension;	
				copy($prod_img,$prod_img_size_1);
				$this->modify_image($prod_img_size_1, $old_width, $old_height, $max_width_1, $max_height_1, $resize_1, $aspect_ratio, $extension);
				
				//size 2
				if($enable_2){
					$prod_img_size_2 = dirname(__FILE__).'/../../../'.$image_dir.'/'.$new_name.'_size2.'.$extension;	
					copy($prod_img,$prod_img_size_2);
					$this->modify_image($prod_img_size_2, $old_width, $old_height, $max_width_2, $max_height_2, $resize_2, $aspect_ratio, $extension);
				}
				
				//size 3
				if($enable_3){
					$prod_img_size_3 = dirname(__FILE__).'/../../../'.$image_dir.'/'.$new_name.'_size3.'.$extension;	
					copy($prod_img,$prod_img_size_3);
					$this->modify_image($prod_img_size_3, $old_width, $old_height, $max_width_3, $max_height_3, $resize_3, $aspect_ratio, $extension);
				}
				
				//size 4
				if($enable_4){
					$prod_img_size_4 = dirname(__FILE__).'/../../../'.$image_dir.'/'.$new_name.'_size4.'.$extension;	
					copy($prod_img,$prod_img_size_4);
					$this->modify_image($prod_img_size_4, $old_width, $old_height, $max_width_4, $max_height_4, $resize_4, $aspect_ratio, $extension);
				}
				
				//size 5
				if($enable_5){
					$prod_img_size_5 = dirname(__FILE__).'/../../../'.$image_dir.'/'.$new_name.'_size5.'.$extension;	
					copy($prod_img,$prod_img_size_5);
					$this->modify_image($prod_img_size_5, $old_width, $old_height, $max_width_5, $max_height_5, $resize_5, $aspect_ratio, $extension);
				}
									
				//delete original image
				if(file_exists($prod_img)){
					unlink($prod_img);	
				}					
				
			}
			$src = $new_name.'_size1.'.$extension;
							
		}//end if an image has been uploaded
		
		
		if($alt=='' && $image_alt_auto){
			if($image_upload==true){	
				$alt_filename = $new_name.'.'.$extension;
			}else{
				$alt_filename = $src_old_name_part.'.'.$src_extension;
			}
			$alt = $alt_filename;
		}
		
		$value = 'src-=-'.$src.'[;-)# ]alt-=-'.$alt.'[;-)# ]';
		if($delete_image){
			$value = '';
		}
		return $value;	
	}	
	
	function modify_image($prod_img, $old_width, $old_height, $new_width, $new_height, $resizetype, $aspect_ratio, $extension){	 
		
		
		if($resizetype=='none'){
			//no resize for this image
			$new_width = $old_width;
			$new_height = $old_height;
			
		}elseif($resizetype=='resize'){	
			
						
				
			//widthratio
			if($new_width){	
				$widthratio = $old_width/$new_width; 
			}else{
				$widthratio = 0;
			}
			
			//height ratio
			if($new_height){			
				$heightratio = $old_height/$new_height;	
			}else{
				$heightratio = 0;
			}	
			
			if($widthratio <= $heightratio){ 
				//vertical image or squere
				$new_width = $old_width/$heightratio;
			}else{ 
				//horizontal image
				$new_height = $old_height/$widthratio;
			}
			
			//round numbers
			$new_width = round($new_width); 
			$new_height = round($new_height); 
			
			if($new_width>$old_width){			
				//no resize for this image
				$new_width = $old_width;
				$new_height = $old_height;
			}
				
		}elseif($resizetype=='force'){
			$new_width = $new_width;
			$new_height = $new_height;
		}		
		
		$imgnew = imagecreatetruecolor($new_width,$new_height);				
		if($extension=='jpg'){
			$srcimg=imagecreatefromjpeg($prod_img);
		}elseif($extension=='gif'){
			$srcimg=imagecreatefromgif($prod_img);
		}elseif($extension=='png'){
			$srcimg=imagecreatefrompng($prod_img);
		}
		if(function_exists('imagecopyresampled')){
			imagecopyresampled($imgnew,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg));
		}else{
			imagecopyresized($imgnew,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg));
		}				
		if($extension=='jpg'){
			imagejpeg($imgnew,$prod_img,90);
		}elseif($extension=='gif'){
			imagegif($imgnew,$prod_img);
		}elseif($extension=='png'){
			imagepng($imgnew,$prod_img);
		}
		
		
		/*
		if($crop){
				
			$left = 0;
			$top = 0;
			$need_cropping = 0;
			if($new_width>$crop_width){
				//need to crop horizontally
				$need_cropping = 1;
				$extra_space = $new_width-$crop_width;							
				$left = round($extra_space/2);							
			}
			
			if($new_height>$crop_height){
				//need to crop vertically
				$need_cropping = 1;
				$extra_space = $new_height-$crop_height;							
				$top = round($extra_space/2);										
			}
			
			if($need_cropping){						
				//set new size
				$canvas = imagecreatetruecolor($crop_width, $crop_height);
				
				//get image
				if($extension=='jpg'){							
					$current_image=imagecreatefromjpeg($prod_img)or die('Problem In opening Source Image');
				}elseif($extension=='gif'){
					$current_image=imagecreatefromgif($prod_img)or die('Problem In opening Source Image');
				}
				
				//do crop	
				imagecopy($canvas, $current_image, 0, 0, $left, $top, $crop_width, $crop_height);
				
				//put image away
				if($extension=='jpg'){
					imagejpeg($canvas,$prod_img,100)or die('Problem In saving');
				}elseif($extension=='gif'){
					imagegif($canvas,$prod_img)or die('Problem In saving');
				}							
			}						
		}//end crop	
		
		
		$size_ratio_upload = $old_width/$old_height;
			
			//if ratio==1 it is a square, if bigger then 1 image is wide, if smaller then 1 image is tall						
			if($size_ratio_upload <= 1){ 
				//vertical image or square							
				
				//resize to the thumbnail-width, so only adjust the height			
				$resize_ratio = $old_width/$new_width;								
				$new_height = $old_height/$resize_ratio;
				if($new_height<$crop_height){
					$resize_ratio = $old_height/$new_height;
					$new_width = $old_width/$resize_ratio;	
					$new_height = $crop_height;
				}	
										
			}else{ 
				//horizontal image
				
				//resize to the thumbnail-height, so only adjust the width			
				$resize_ratio = $old_height/$new_height;
				$new_width = $old_width/$resize_ratio;	
				if($new_width<$crop_width){
					$resize_ratio = $old_width/$new_width;								
					$new_height = $old_height/$resize_ratio;
					$new_width = $crop_width;
				}	
											
			}
		*/
	}
	
	function delete_image($temp_old_src, $image_dir){
		$temp_old_src_array = explode('.',$temp_old_src);
		$temp_old_src_name = $temp_old_src_array[0];	
		$temp_old_src_name = explode('_size',$temp_old_src_name);			
		$temp_old_src_name = $temp_old_src_name[0];		
		$temp_old_src_extension = $temp_old_src_array[1];	
		if(file_exists(dirname(__FILE__).'/../../../'.$image_dir.'/'.$temp_old_src_name.'_size1.'.$temp_old_src_extension)){			
			unlink(dirname(__FILE__).'/../../../'.$image_dir.'/'.$temp_old_src_name.'_size1.'.$temp_old_src_extension);				
		}			
		if(file_exists(dirname(__FILE__).'/../../../'.$image_dir.'/'.$temp_old_src_name.'_size2.'.$temp_old_src_extension)){
			unlink(dirname(__FILE__).'/../../../'.$image_dir.'/'.$temp_old_src_name.'_size2.'.$temp_old_src_extension);	
		}
		if(file_exists(dirname(__FILE__).'/../../../'.$image_dir.'/'.$temp_old_src_name.'_size3.'.$temp_old_src_extension)){
			unlink(dirname(__FILE__).'/../../../'.$image_dir.'/'.$temp_old_src_name.'_size3.'.$temp_old_src_extension);	
		}
		if(file_exists(dirname(__FILE__).'/../../../'.$image_dir.'/'.$temp_old_src_name.'_size4.'.$temp_old_src_extension)){
			unlink(dirname(__FILE__).'/../../../'.$image_dir.'/'.$temp_old_src_name.'_size4.'.$temp_old_src_extension);	
		}
		if(file_exists(dirname(__FILE__).'/../../../'.$image_dir.'/'.$temp_old_src_name.'_size5.'.$temp_old_src_extension)){
			unlink(dirname(__FILE__).'/../../../'.$image_dir.'/'.$temp_old_src_name.'_size5.'.$temp_old_src_extension);	
		}
	}
	
	//function to delete images when item is deleted, from PI version 1.4.7
	function item_delete($item_id, $type_id, $field){	
	
		//get image dir
		$image_dir = $this->get_field_param($field->params, 'image_dir');	
		$image_dir = $this->alter_image_dir($image_dir, $item_id);		
		
		$field_id = $field->id;			
		
		//get value of image
		$this->db->setQuery("SELECT value "
		."FROM #__pi_custom_fields_values "
		."WHERE (field_id='$field_id' AND item_id='$item_id') "
		."LIMIT 1 "
		);
		$field_rows = $this->db->loadObjectList();
		foreach($field_rows as $field_row){	
			$field_value = $field_row->value;
		}
		$images_array = explode('[;-)# ]',$field_value);
		$image_stuff = $images_array[0];		
		$image_stuff_array = explode('-=-',$image_stuff);
		$image = $image_stuff_array[1];		
		if($image){
			$this->delete_image($image, $image_dir);
		}
		
	}	
	
	//function to delete images when field or itemtype is deleted, from PI version 1.4.7
	function field_delete($field){
	
		//get image dir
		$image_dir = $this->get_field_param($field->params, 'image_dir');
		
	
		$field_id = $field->id;			
		
		//get values of images
		$this->db->setQuery("SELECT item_id, value "
		."FROM #__pi_custom_fields_values "
		."WHERE field_id='$field_id' "
		);
		$field_rows = $this->db->loadObjectList();
		foreach($field_rows as $field_row){	
			$field_value = $field_row->value;	
			$item_id = $field_row->item_id;			
		}	
		
		
		$images_array = explode('[;-)# ]',$field_value);		
		$image_stuff = $images_array[0];		
		$image_stuff_array = explode('-=-',$image_stuff);
		$image = $image_stuff_array[1];		
		if($image){
			$image_dir = $this->alter_image_dir($image_dir, $item_id);	
			$this->delete_image($image, $image_dir);
		}			
	}
	
}

?>